Learn Qt

Teaching Qt® to the world!

Flower

The Qt Build System

One aspect of using Qt is to understand how it works. Signals and slots are great – and there are more nice features: resources, properties, meta types, etc. What is important here is that Qt is still all about C++. Writing a Qt application means writing a C++ application – Qt does not change the language.

So, how does all of this work? Let’s start by having a look at how a C++ application is built.

The C++ build system

The C++ build system

The source code of a C++ application can be divided into headers (*.h) and sources (*.cpp). There are other extensions such as hxx, hpp, hh, cc, cxx and so on, but I hope that you get the picture. The headers are included by the sources, the sources are compiled into objects and these objects are linked to a executable (or library, etc).

So, what does Qt add to this to let us write nice things as signals and slots? First, macros, meaning that what you write is transformed into plain vanilla C++. Secondly, code generators, completing your application with the C++ code needed to make things work smoothly. Let’s start by looking at the big picture.

The Qt build system

The Qt build system

Comparing the C++ build system to the Qt build system, you can see that the C++ build system, i.e. the gray boxes, are left unmodified. We are still building C++ code here. However, we add more sources and headers. There are three code generators involved here:

The meta-object compiler (moc in the illustration) – the meta-object compiler takes all classes starting with the Q_OBJECT macro and generates a moc_*.cpp C++ source file. This file contains information about the class being moc’ed such as class name, inheritance tree, etc, but also implementation of the signals. This means that when you emit a signal, you actually call a function generated by the moc.

The user interface compiler (uic in the illustration) – The user interface compiler takes designs from Designer and creates header files. These header files are then included into source files as usual, making it possible to call setupUi to instanciate a user interface design.

The Qt resource compiler (rcc in the illustration) – The resource compiler is something we have not talked about yet. It makes it possible to embedd images, text files, etc into your executable, but still to access them as files. We will look at this later, I just want to include it in this picture where it belongs.

I hope these two illustrations clarifies what Qt really does to add new nice keywords to C++. If you are curious – feel free to read some of the generated files. Just don’t alter them – they are regenerated each time you build your application.

fact-logoIf you are using QtCreator, the moc files are generated in the debug and release sub-directories of your project directory. The uic files are stored in the root of the project directory. The rcc files are generally boring, but I’m sure that you can find them in your project directory hierarcy somewhere.

Be Sociable, Share!

Leave a Reply

You must be logged in to post a comment.