Qt

Building Qt quickly

The fastest way to build Qt on Windows is: Grab Jom from the Nokia FTP site. Put it into the folder you extracted the Qt source to. Then open a Visual Studio command prompt and run:

configure -mp -opensource -nomake demos -nomake examples
jom && jom clean

Adding transactional safety to drag & drop operations

If you use a database backend, it’s sometimes useful to start a transaction around a drag & drop operation. However, subclassing the widget and starting on dragMoveEvent, aborting on dragLeaveEvent and committing on dropEvent is not enough, as dropEvent is raised before operations on the view have happened (for instance, a QTreeWidget will call removeRows after the dropEvent has been raised.) The easiest way to overcome this is to reimplement startDrag() and call the parent implementation inside before committing the transaction. After startDrag() has finished, the widget is done processing and the model changes are finished.

Building Qt projects with CMake

Resources

.qrc contain resources, and are the easiest to handle. The Qt resource compiler (rcc) simply creates a single .cpp file, and puts all resources as static arrays into it. The corresponding CMake function is QT4_ADD_RESOURCES. The first parameter is the output, which you have to add to your source file list.

UI

For the .ui files, the UI-compiler has to be called first (uic) which generates a header file; this header contains all the logic to set up your user interface. You include this generated header in your source files, and use it to display the UI. How does this look like in practice? Well, let’s assume we have a file “testDialog.ui“, which contains the UI, and we want to implement a dialog using this UI. We create a custom class, for example, TestDialog, which is derived from QDialog. This class is put into TestDialog.h/.cpp, and may look like this:

#ifndef MY_TEST_DIALOG_H
#define MY_TEST_DIALOG_H

#include "ui_TestDialog.h"

class TestDialog : public QDialog
{
    Q_OBJECT

public:
    TestDialog ();

public slots:
public signals:

private:
    Ui::TestDialog ui;
};

#endif

The “ui_TestDialog.h” file is generated by the user interface compiler (uic), using the QT4_WRAP_UI command (What you want is QT4_WRAP_UI(UI_SOURCES ${UI_FILES}) and then include the UI_SOURCES into your project — the UI_SOURCES are the generated header files.) The output is directly into the output folder, so you should add INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) to your project file — otherwise, the files won’t be found. In your dialog constructor, you can init the UI now using:

ui.setupUi (this);

QObject derived

Running the meta-object compiler for QObject derived classes is similar to resources, you add QT4_WRAP_CPP in your CMake file, and just add the generated files to your source files. The input for this function is the header of your class, not the implementation — even though it’s called QT4_WRAP_CPP, you must pass the file containing the declaration; this is usually the header. Those files contain a bunch of functions which are needed for the Qt meta-object system. These are stand-alone .cpp files that you usually never have to look at.

Comments are closed.