next up previous contents
Next: Ressources Up: Compiling Your First Qt Previous: Hello, World   Contents

Subsections

A (slightly) more complex application

As mentioned above, you will eventually encounter much more complex projects consisting of multiple files, including your own customary header-files. Since Qt utilizes some macros that need to be pre-processed before compilation, the procedure to build such a project is slightly different from the above.

The header files, or any other file that contains some of the Qt-specific macros, need to be processed by moc, the Meta-Object Compiler, before they can be compiled by g++.

So let's assume we modify the code from above as follows. First, we create a file called ``main.cpp'':

#include <qapplication.h>
#include "myButton.h"

int main( int argc, char **argv )
{
    QApplication a( argc, argv );

    myButton button( "Hello World!");

    a.setMainWidget( &button );
    button.show();
    return a.exec();
}

Here, we are making use of a class called ``myButton'', which is defined in the header ``myButton.h'':

#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <qpushbutton.h>

class myButton : public QPushButton
{
    Q_OBJECT
        public:
            myButton(const QString& text, QWidget *parent=0);
};

#endif

The actual code for all of ``myButton'''s functionality goes into ``myButton.cpp'':

#include "myButton.h"

myButton::myButton(const QString& text, QWidget *parent) 
             : QPushButton(text, parent)
{
        resize( 100, 30 );
        connect(this, SIGNAL(clicked()), this, SLOT(close()));
}

Granted, this does not do very much and could easily be achieved in our first example in a much more obvious way. However, let's take this example to show how to compile a program that consists of several files. Now we have:

  1. main.cpp - the main sources
  2. myButton.h - defining the ``myButton'' class
  3. myButton.cpp - implementation code of ``myButton''

Compiling by hand

Again, just to teach you what exactly is happening here, I will walk you through the steps necessary to compile and link this application. Once you understand what is going on here, you should however take advantage of ``tmake'' as described below.

The first step is to compile the .cpp files, which is identical to the commands used above:

    $> g++ -Wall -I$QTDIR/include -c myButton.cpp
    $> g++ -Wall -I$QTDIR/include -c main.cpp

Now, before we can continue and link the object files together, we need to use the meta-object compiler to process the header files:

    $> moc myButton.h -o myButton.moc.cpp

This creates a new .cpp file, which in turn we need to compile into an object file:

    $> g++ -Wall -I$QTDIR/include -c myButton.moc.cpp

Finally, we have all our object files and continue to link the application similar to our first example:

    $> g++ -Wall -L$QTDIR/lib myButton.o main.o myButton.moc.o -lqt -o hello

Using tmake

Now that we understand the commands necessary, we can take advantage of the power of make and use ``tmake'' to create the initial Makefile. The input to tmake, ``hello.pro'' will now look as follows:

HEADERS    = myButton.h
SOURCES    = myButton.cpp main.cpp
TARGET     = hello

Run tmake and make just as in the example above:

    $> tmake hello.pro -o Makefile
    $> make

And once again: voilà!


next up previous contents
Next: Ressources Up: Compiling Your First Qt Previous: Hello, World   Contents
Jan Schaumann 2001-12-26