Google

Generating C++ Source Code From The Qt Designer File

As you might have guessed from the filename extension .ui, Qt Designer does not generate C++ source code right away. While this would be possible, it would make it much more difficult to change the dialogs later by using Qt Designer.

Out of curiosity, you might be wondering what it is that Qt Designer saves in its .ui files. These files are a textual description of the dialog you just created. The format used is the markup language XML. Here is what pizza.ui can look like; your file might have slightly different values for sizes and positions:

<!DOCTYPE UI><UI>
<class>PizzaEntry</class>
<widget>
    <class>QDialog</class>
    <property>
        <name>name</name>
        <cstring>PizzaEntry</cstring>
    </property>
    <property>
        <name>geometry</name>
        <rect>
            <x>0</x>
            <y>0</y>
            <width>310</width>
            <height>222</height>
        </rect>
    </property>
    <property>
        <name>caption</name>
        <string>Form1</string>
    </property>
    <widget>
        <class>QButtonGroup</class>
        <property>
            <name>name</name>
            <cstring>ButtonGroup1</cstring>
        </property>
        <property>
            <name>geometry</name>
            <rect>
                <x>30</x>
                <y>10</y>
                <width>122</width>
                <height>119</height>
            </rect>
        </property>
        <property>
            <name>title</name>
            <string>Toppings</string>
        </property>
        <widget>
            <class>QCheckBox</class>
            <property>
                <name>name</name>
                <cstring>CheckBox1</cstring>
            </property>
            <property>
                <name>geometry</name>
                <rect>
                    <x>10</x>
                    <y>20</y>
                    <width>102</width>
                    <height>19</height>
                </rect>
            </property>
            <property>
                <name>text</name>
                <string>Mushrooms</string>
            </property>
        </widget>
        <widget>
            <class>QCheckBox</class>
            <property>
                <name>name</name>
                <cstring>CheckBox2</cstring>
            </property>
            <property>
                <name>geometry</name>
                <rect>
                    <x>10</x>
                    <y>40</y>
                    <width>51</width>
                    <height>19</height>
                </rect>
            </property>
            <property>
                <name>text</name>
                <string>Ham</string>
            </property>
        </widget>
        <widget>
            <class>QCheckBox</class>
            <property>
                <name>name</name>
                <cstring>CheckBox3</cstring>
            </property>
            <property>
                <name>geometry</name>
                <rect>
                    <x>10</x>
                    <y>60</y>
                    <width>88</width>
                    <height>19</height>
                </rect>
            </property>
            <property>
                <name>text</name>
                <string>Pineapple</string>
            </property>
        </widget>
        <widget>
            <class>QCheckBox</class>
            <property>
                <name>name</name>
                <cstring>CheckBox4</cstring>
            </property>
            <property>
                <name>geometry</name>
                <rect>
                    <x>10</x>
                    <y>80</y>
                    <width>79</width>
                    <height>19</height>
                </rect>
            </property>
            <property>
                <name>text</name>
                <string>Anchovies</string>
            </property>
        </widget>
    </widget>
    <widget>
        <class>QButtonGroup</class>
        <property>
            <name>name</name>
            <cstring>ButtonGroup2</cstring>
        </property>
        <property>
            <name>geometry</name>
            <rect>
                <x>180</x>
                <y>20</y>
                <width>100</width>
                <height>99</height>
            </rect>
        </property>
        <property>
            <name>title</name>
            <string>Size</string>
        </property>
        <widget>
            <class>QRadioButton</class>
            <property>
                <name>name</name>
                <cstring>RadioButton1</cstring>
            </property>
            <property>
                <name>geometry</name>
                <rect>
                    <x>10</x>
                    <y>20</y>
                    <width>56</width>
                    <height>19</height>
                </rect>
            </property>
            <property>
                <name>text</name>
                <string>Junior</string>
            </property>
        </widget>
        <widget>
            <class>QRadioButton</class>
            <property>
                <name>name</name>
                <cstring>RadioButton2</cstring>
            </property>
            <property>
                <name>geometry</name>
                <rect>
                    <x>10</x>
                    <y>40</y>
                    <width>71</width>
                    <height>19</height>
                </rect>
            </property>
            <property>
                <name>text</name>
                <string>Standard</string>
            </property>
        </widget>
        <widget>
            <class>QRadioButton</class>
            <property>
                <name>name</name>
                <cstring>RadioButton3</cstring>
            </property>
            <property>
                <name>geometry</name>
                <rect>
                    <x>10</x>
                    <y>60</y>
                    <width>58</width>
                    <height>19</height>
                </rect>
            </property>
            <property>
                <name>text</name>
                <string>Family</string>
            </property>
        </widget>
    </widget>
    <widget>
        <class>QCheckBox</class>
        <property>
            <name>name</name>
            <cstring>CheckBox5</cstring>
        </property>
        <property>
            <name>geometry</name>
            <rect>
                <x>110</x>
                <y>150</y>
                <width>101</width>
                <height>19</height>
            </rect>
        </property>
        <property>
            <name>text</name>
            <string>Extra Cheese</string>
        </property>
    </widget>
    <widget>
        <class>QPushButton</class>
        <property>
            <name>name</name>
            <cstring>PushButton1_2_2</cstring>
        </property>
        <property>
            <name>geometry</name>
            <rect>
                <x>50</x>
                <y>180</y>
                <width>108</width>
                <height>32</height>
            </rect>
        </property>
        <property>
            <name>text</name>
            <string>OK</string>
        </property>
    </widget>
    <widget>
        <class>QPushButton</class>
        <property>
            <name>name</name>
            <cstring>PushButton1_2</cstring>
        </property>
        <property>
            <name>geometry</name>
            <rect>
                <x>170</x>
                <y>180</y>
                <width>108</width>
                <height>32</height>
            </rect>
        </property>
        <property>
            <name>text</name>
            <string>Cancel</string>
        </property>
    </widget>
</widget>
</UI>

The first few lines specify that this is a Qt Designer file and that it describes a dialog that should be called PizzaEntry. The rest describe the individual widgets by listing their names, their classes, and some of their properties. Only those properties that are different from the default—i.e., those values that were automatically set when you created the widget in the form—are listed here. You could in theory change values here, but this is not recommended, since you might break the format if you do not know exactly what you are doing. It's better to edit these files with Qt Designer.

Of course, no C++ compiler can use such a file, so you will have to convert it first. This is the task of another program that comes with Qt Designer: uic, the user interface compiler.

You need to call uic twice to generate both the header file and the implementation file of your dialog class. Assuming that uic is in your search path, you can generate the header file as follows:

uic -o PizzaEntry.h pizza.ui

The option -o tells uic how to name the header file, and pizza.ui is the name of the file you saved with Qt Designer. As with the XML file, let's have a brief look at the generated file:

/****************************************************************************
** Form interface generated from reading ui file 'pizza.ui'
**
** Created: Tue Jun 20 13:22:32 2000
**      by:  The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/

#ifndef PIZZAENTRY_H
#define PIZZAENTRY_H

#include <qdialog.h>

class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QButtonGroup;
class QCheckBox;
class QPushButton;
class QRadioButton;

class PizzaEntry : public QDialog
{
    Q_OBJECT

public:
    PizzaEntry( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags f = 0 );
    ~PizzaEntry();

    QButtonGroup* ButtonGroup1;
    QCheckBox* CheckBox1;
    QCheckBox* CheckBox2;
    QCheckBox* CheckBox3;
    QCheckBox* CheckBox4;
    QButtonGroup* ButtonGroup2;
    QRadioButton* RadioButton1;
    QRadioButton* RadioButton2;
    QRadioButton* RadioButton3;
    QCheckBox* CheckBox5;
    QPushButton* PushButton1_2_2;
    QPushButton* PushButton1_2;
protected:
};

#endif // PIZZAENTRY_H

If you have programmed with Qt before, the only thing that might surprise you here is the fact that all the widget members of the dialog are made public. We'll discuss later why this is so.

The next step is to create the implementation file. You tell uic that you want to create an implementation file instead of a header file by specifying the option -i and passing the name of the already generated header file:

uic -i PizzaEntry.h -o PizzaEntry.cpp pizza.ui

As before, the option -o determines the name of the generated file. Again, let's look at the result:

/****************************************************************************
** Form implementation generated from reading ui file 'pizza.ui'
**
** Created: Tue Jun 20 13:23:00 2000
**      by:  The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/

#include "PizzaEntry.h"
#include <qbuttongroup.h>
#include <qcheckbox.h>
#include <qpushbutton.h>
#include <qradiobutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>

/*
 *  Constructs a PizzaEntry which is a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  TRUE to construct a modal dialog.
 */

PizzaEntry::PizzaEntry( QWidget* parent,  const char* name, bool modal, WFlags f )
    : QDialog( parent, name, modal, f )
{
    if ( !name )
        setName( "PizzaEntry" );

    resize( 310, 222 );

    setCaption( tr( "Form1"  ) );

    ButtonGroup1 = new QButtonGroup( this, "ButtonGroup1" );
    ButtonGroup1->setGeometry( QRect( 30, 10, 122, 119 ) );
    ButtonGroup1->setTitle( tr( "Toppings"  ) );

    CheckBox1 = new QCheckBox( ButtonGroup1, "CheckBox1" );
    CheckBox1->setGeometry( QRect( 10, 20, 102, 19 ) );
    CheckBox1->setText( tr( "Mushrooms"  ) );

    CheckBox2 = new QCheckBox( ButtonGroup1, "CheckBox2" );
    CheckBox2->setGeometry( QRect( 10, 40, 51, 19 ) );
    CheckBox2->setText( tr( "Ham"  ) );

    CheckBox3 = new QCheckBox( ButtonGroup1, "CheckBox3" );
    CheckBox3->setGeometry( QRect( 10, 60, 88, 19 ) );
    CheckBox3->setText( tr( "Pineapple"  ) );

    CheckBox4 = new QCheckBox( ButtonGroup1, "CheckBox4" );
    CheckBox4->setGeometry( QRect( 10, 80, 79, 19 ) );
    CheckBox4->setText( tr( "Anchovies"  ) );

    ButtonGroup2 = new QButtonGroup( this, "ButtonGroup2" );
    ButtonGroup2->setGeometry( QRect( 180, 20, 100, 99 ) );
    ButtonGroup2->setTitle( tr( "Size"  ) );

    RadioButton1 = new QRadioButton( ButtonGroup2, "RadioButton1" );
    RadioButton1->setGeometry( QRect( 10, 20, 56, 19 ) );
    RadioButton1->setText( tr( "Junior"  ) );

    RadioButton2 = new QRadioButton( ButtonGroup2, "RadioButton2" );
    RadioButton2->setGeometry( QRect( 10, 40, 71, 19 ) );
    RadioButton2->setText( tr( "Standard"  ) );

    RadioButton3 = new QRadioButton( ButtonGroup2, "RadioButton3" );
    RadioButton3->setGeometry( QRect( 10, 60, 58, 19 ) );
    RadioButton3->setText( tr( "Family"  ) );

    CheckBox5 = new QCheckBox( this, "CheckBox5" );
    CheckBox5->setGeometry( QRect( 110, 150, 101, 19 ) );
    CheckBox5->setText( tr( "Extra Cheese"  ) );

    PushButton1_2_2 = new QPushButton( this, "PushButton1_2_2" );
    PushButton1_2_2->setGeometry( QRect( 50, 180, 108, 32 ) );
    PushButton1_2_2->setText( tr( "OK"  ) );

    PushButton1_2 = new QPushButton( this, "PushButton1_2" );
    PushButton1_2->setGeometry( QRect( 170, 180, 108, 32 ) );
    PushButton1_2->setText( tr( "Cancel"  ) );
}

/*
 *  Destroys the object and frees any allocated resources
 */

PizzaEntry::~PizzaEntry()
{
    // no need to delete child widgets, Qt does it all for us
}

You might feel that this source code is a bit more verbose than handcrafted code, but this is always the price you pay for the convenience of machine-generated code.