QtNpp: Notepad++ Plugin to build Qt Applications


I really love Qt and began to learn it recently, but I didn’t find a way to build Qt files from inside Notepad++ so as usual I made mine 😉

QtNpp is my Notepad++ plugin to build and run Qt applications.

Notepad++  Qt

The idea

The idea of QtNpp is simple; just build the file by using these lines:

@echo off
set QTDIR=%1
set PATH=C:\Qt\2009.04\qt\bin;C:\Qt\2009.04\bin;C:\Qt\2009.04\mingw\bin;C:\WINDOWS\System32;%PATH%
set QMAKESPEC=win32-g++
qmake -project
qmake
mingw32-make.exe

but the implementation of QtNpp is not that simple!

By default QtNpp try to build Qt files by using the above script which works in one of these cases:

  1. You have Qt opensource edition installed at the default path: C:\Qt\2009.04
  2. You have Qt opensource edition installed at another path and this installation path was added to PATH system variables.
  3. You have Qt Commercial edition and you manually installed MingW and both installation paths was added to PATH system variables.

What if that is not the case? If my QtNpp doesn’t work by default, don’t worry you can change the building script yourself!

Just create QtNpp.ini in <Notepad++ installation folder>/plugins/Config and QtNpp will execute QtNpp.ini file as a batch and passing the folder of Qt file and the filename to it.

Yes, that is you can put the above script in QtNpp.ini and change whatever you want to change in it, and QtNpp will execute your new script instead of its one!

Looking inside

I used this template as a start of my QtNpp, also I read the source of some free Notepad++ plugins to learn from.

The build and run function of QtNpp is:

void buildAndRun()
{
	//Save current opened file first
	::SendMessage(nppData._nppHandle, NPPM_SAVECURRENTFILE, NULL,NULL);
	//Get directory and filename of the opened file
	String directory = getCurrentFilePath();
	String filename = getCurrentFileName();
	String command = getBuildCommand(filename,directory,true);
	//Build and run opened file
	RunCommand(filename,directory,command);
}

Here I get the build commands by getBuildCommand and then run these commands with RunCommand function, which replace %1 with directory path and %2 with filename and save them in bat file and execute it by calling ExecuteBatch function:

String ExecuteBatch (String filename,String directory,const char* path)
{
	CreateRedirectedProcess((LPTSTR)TEXT(path),(LPTSTR)TEXT(directory.c_str()));
	String s=ReadFromPipe(showText);
	return s;
}

You can see that I run the batch file in a new process and then read its output from the redirected pipe and send it to NppExec console to print it (which is a Notepad++ plugin comes by default with Notepad++).

int CreateRedirectedProcess(LPTSTR szCmdline,LPTSTR directory)
{
   SECURITY_ATTRIBUTES saAttr;

   saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
   saAttr.bInheritHandle = TRUE;
   saAttr.lpSecurityDescriptor = NULL;

   if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) )
      ErrorExit(TEXT("StdoutRd CreatePipe"));

   if ( ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) )
      ErrorExit(TEXT("Stdout SetHandleInformation"));

   if (! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
      ErrorExit(TEXT("Stdin CreatePipe"));

   if ( ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
      ErrorExit(TEXT("Stdin SetHandleInformation"));

   CreateChildProcess(szCmdline,directory);

   return 0;
}

void CreateChildProcess(LPTSTR szCmdline,LPTSTR directory)
{

   STARTUPINFO siStartInfo;
   BOOL bSuccess = FALSE;
   ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
   ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
   siStartInfo.cb = sizeof(STARTUPINFO);
   siStartInfo.hStdError = g_hChildStd_OUT_Wr;
   siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
   siStartInfo.hStdInput = g_hChildStd_IN_Rd;
   siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
   siStartInfo.wShowWindow=SW_HIDE;

   // Create the child process.
   bSuccess = CreateProcess(NULL,
      szCmdline,     // command line
      NULL,          // process security attributes
      NULL,          // primary thread security attributes
      TRUE,          // handles are inherited
      CREATE_NO_WINDOW,             // creation flags
      NULL,          // use parent's environment
      directory,          // use parent's current directory
      &siStartInfo,  // STARTUPINFO pointer
      &piProcInfo);  // receives PROCESS_INFORMATION

   if ( ! bSuccess )
      ErrorExit(TEXT("CreateProcess"));
   else
   {
      CloseHandle(piProcInfo.hProcess);
      CloseHandle(piProcInfo.hThread);
   }
}

Here I redirect the output by creating a pipe and pass it to process using siStartInfo.hStdOutput. Also I hide the console window by using CREATE_NO_WINDOW flag in CreateProcess.

Configure QtNpp

As I said above if QtNpp doesn’t work by default with you, just create QtNpp.ini and put all commands you need to build Qt on your PC in it.

I suggest to take these QtNpp.ini as a start:

QtNpp.ini for MingW

@echo off
set QTDIR=%1
set PATH=C:\Qt\2009.04\qt\bin;C:\Qt\2009.04\bin;C:\Qt\2009.04\mingw\bin;C:\WINDOWS\System32;%PATH%
set QMAKESPEC=win32-g++
qmake -project
qmake
mingw32-make.exe

QtNpp.ini for Visual Studio

@echo off
set QTDIR=%1
set PATH=C:\Qt\4.4.3\bin;C:\Program Files\Microsoft Visual Studio 9.0\VC\bin;%PATH%
set QMAKESPEC=win32-msvc2008
call “C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat”
qmake -project
qmake
nmake

Just replace fixed paths in QtNpp.ini with paths on your computer or add them to PATH system variable.

You may notice that you can build any kind of files with this plugin (not only Qt) because when you make QtNpp.ini, my plugin turns into executer for it only!

Download

You can download QtNpp dll and sourcecode under GNU License:

QtNpp dll ANSI QtNppPlugin_ansi.zip
QtNpp dll Unicode QtNppPlugin_unicode.zip
QtNpp source QtNpp_src.zip
QtNpp.ini for MingW QtNpp_MingW.zip
QtNpp.ini for VS QtNpp_VS.zip
License GNU General Public License
Advertisements

4 Responses to “QtNpp: Notepad++ Plugin to build Qt Applications”

  1. Building Qt Applications using your favorite text editor « Madhat Alsoos Says:

    […] You can download it (and even change the way it build your Qt Applications) from my post “QtNpp: Notepad++ Plugin to build Qt Applications”. […]

  2. Arul Says:

    I have installed 32bit Qt Creator in my Linux OS(Debian), but now unable to open the Qt Creator please help me.

    • Madhat Alsoos Says:

      I didn’t try Qt Creator in Linux, but it works fine for me on Windows XP and Windows 7.
      What happened with you when you try to open Qt Creator on your Linux?

  3. Kolobok Says:

    This is what version? For v5.9 does not work.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: