Integrating Qt Designer with Visual Studio

Qt Designer can be integrated into Visual Studio using the qmsdev.dsp file that is supplied with Qt.

Start up Visual Studio and click File|Open Workspace. Open %QTDIR%\tools\designer\integration\qmsdev\qmsdev.dsp. Click Build|Set Active Configuration and in the list click 'QMsDev - Win32 Release', then click OK. Now click Build|Build qmsdev.dll. You should now copy the file %QTDIR%\tools\designer\integration\qmsdev\Release\qmsdev.dll into \Program Files\Microsoft Visual Studio\Common\MSDev98\AddIns. Now click Tools|Customize. Click the Add-in Macro Files tab, then click the Browse button. Change the file type to 'Add-ins (.dll)' and navigate to \Program Files\Microsoft Visual Studio\Common\MSDev98\AddIns. Click the qmsdev.dll file, click Open, then click Close.

A new toolbar will appear in Visual Studio with the following toolbar buttons:

Double clicking a .ui file in the workspace overview will now launch Qt Designer.

If you create a .cpp file which contains the Q_OBJECT macro you will need an additional file which is generated by the moc to be included in your project. For example, if you have 'file.cpp', then the last line would be #include "file.moc" and the additional file would be called 'file.moc'. To ensure that Visual Studio executes the moc and generates this file you must create a custom dependency. Double click the .cpp file (in your project workspace) that contains the Q_OBJECT macro. Click the Add MOC toolbar button; this will create an empty .moc file in your project workspace. Right click the newly created .moc file, then click Settings from the pop-up menu to invoke the Project Settings dialog. Click the Custom Build tab. Click the Dependencies button to pop up the User Defined Dependencies dialog. Type in $(InputDir)\$(InputPath), then press Return. Click OK to leave the Dependencies dialog, then click OK to leave the Project Settings dialog.

If you wish to delete the add-in remove it from the toolbar then delete the qmsdev.dll file from the add-ins directory.

Creating Makefiles without qmake

The qmake tool provided with Qt can create Makefiles appropriate to your platform based on .pro project files. This section describes the dependencies involved in building a Qt application and gives a couple of simple example Makefiles. This section assumes that you have a good understanding of Makefiles.

Qt Designer produces .ui files which are used to generate .h and .cpp files for the compiler to compile. The .ui files are processed by uic. Classes which inherit from QObject, e.g. those which use slots and signals, require an additional .cpp file to be generated. These files are generated by the moc and are named 'moc_file.cpp' where the original .cpp file is called 'file.cpp'. If your .cpp file contains the Q_OBJECT macro an additional file 'file.moc' should be generated which must be #included in the .cpp, normally at the end. This requires an extra dependency being created.

Processing .ui files with uic is done twice:
uic myform.ui -o myform.h
uic myform.ui -i myform.h -o myform.cpp
The first execution creates the header file, the second creates the .cpp file. If you wish to subclass a form you can use uic to generate subclass skeletons:
uic formbase.ui -o formbase.h
uic formbase.ui -i formbase.h -o formbase.cpp
uic -subdecl Form formbase.h formbase.ui -o form.h
uic -subimpl Form formbase.h formbase.ui -o form.cpp
First we generate the header and implementation file for our base class. Then we generate the header and implementation skeletons for our subclass. Note that the use of uic to generate skeletons is not something that would be done in a Makefile, we mention it here because it can be useful for command line users.

For implementation files that contain classes which inherit from QObject we must create moc files:
moc myform.h -o moc_myform.cpp

We'll look at a simple Makefile to see the dependencies in practice.
myapp: moc_myform.o myform.o main.o
        g++ -lqt -o myapp moc_myform.o myform.o main.o

main.o: main.cpp
        g++ -o main.o main.cpp

moc_myform.o: moc_myform.cpp
        g++ -o moc_myform.o moc_myform.cpp

moc_myform.cpp: myform.h
        moc myform.h -o moc_myform.cpp

myform.o: myform.cpp
        g++ -o myform.o myform.cpp

myform.cpp: myform.h myform.ui
        uic myform.ui -i myform.h -o myform.cpp

myform.h: myform.ui
        uic myform.ui -o myform.h
Note that you may need to include the full path to the commands in your Makefile, and under Windows the filenames are moc.exe and uic.exe.

In Unix/Linux environments the make command may be able to do more for us, so we should be able to use a simpler Makefile like this:
myapp: moc_myform.o myform.o main.o
        g++ -lq -o $@ $^

%.o: %.cpp
        g++ -o $^ $@

moc_%.cpp: %.h
        moc $^ -o $@

myform.cpp: myform.h myform.ui
        uic myform.ui -i myform.h -o myform.cpp

myform.h: myform.ui
        uic myform.ui -o myform.h
To see more sophisticated Makefiles simply generate them using qmake on any of your Qt projects or any of the examples supplied with Qt.