Compiling and Building an Application

So far we have written about 99% of a Qt application entirely in Qt Designer. To make the application compile and run we must create a main.cpp file from which we can call our form. We'll then need to update the project file accordingly and generate a Makefile. We can use the Makefile to generate the application. We will cover these matters briefly here, and in more detail in the section called Subclassing in Chapter 4 "Subclassing".

First we need to create a main.cpp file.
#include <qapplication.h>
#include "multiclip.h"
int main( int argc, char *argv[] ) 
{
    QApplication app( argc, argv );

    MulticlipForm clippingForm;
    app.setMainWidget( &clippingForm );
    clippingForm.show();

    return app.exec();
}
The program creates a QApplication object and an instance of our MulticlipForm, sets the form to be the main widget and shows the form. The app.exec() call starts off the event loop.

We need to add main.cpp to the .pro file. Open multiclip.pro in a plain text editor (such as notepad, vim, xemacs, etc.). It will look similar to this:
TEMPLATE    = app
CONFIG     += qt warn_on release
TARGET      = multiclip
INTERFACES  = multiclip.ui
IMAGEFILE   = images.cpp
PROJECTNAME = multiclip
LANGUAGE    = C++
SOURCES    += images.cpp
Add a new line at the end of the file:
SOURCES += main.cpp
Save the updated project file. Now start up a console (or xterm), change directory to the multiclip application and run qmake. A Makefile compatible with your system will be generated:
qmake -o Makefile multiclip.pro
You can now make the application, e.g. by running make or nmake. Try compililng and running multiclip. There are many improvement you could make and experimenting with both the layout and the code will help you learn more about Qt and Qt Designer.

This chapter has introduced you to creating cross-platform applications with Qt Designer. We've created a form, populated it with widgets and laid the widgets out neatly and scalably. We've used Qt's signals and slots mechanism to make the application functional and generated the Makefile. These techniques for adding widgets to a form and laying them out with the layout tools; and for creating, coding and connecting slots will be used time and again as you create applications with Qt Designer. The following chapters will present further examples and explore more techniques for using Qt Designer.