Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > a6711891ce757817bba854bf3f25205a > files > 1878

qtjambi-doc-4.3.3-3mdv2008.1.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /home/gvatteka/dev/qt-4.3/doc/src/porting4-designer.qdoc -->
<head>
  <title>Porting .ui Files to Qt 4</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Porting .ui Files to Qt 4<br /><small></small></h1>
<p>Qt Designer has changed significantly in the Qt 4 release. We have moved away from viewing Qt Designer as an IDE and concentrated on creating a robust form builder which can be extended and embedded in existing IDEs. Our efforts are ongoing and include the Visual Studio Integration</tt>, as well as integrating Designer with KDevelop and possibly other IDEs.</p>
<p>The most important changes in Qt Designer 4 which affect porting for <tt>.ui</tt> files are summarized below:</p>
<ul>
<li><b>Removed project manager.</b> Qt Designer now only reads and edits <tt>.ui</tt> files. It has no notion of a project (<tt>.pro</tt> file).</li>
<li><b>Removed code editor.</b> Qt Designer can no longer be used to edit source files.</li>
<li><b>Changed format of <tt>.ui</tt> files.</b> Qt Designer 4 cannot read files created by Qt Designer 3 and vice versa. However, we provide the tool <tt>uic3</tt> to generate Qt 4 code out of Qt 3 <tt>.ui</tt> files, and to convert old <tt>.ui</tt> files into a format readable by Qt Designer 4.</li>
<li><b>Changed structure of the code generated by <tt>uic</tt>.</b> The <tt>myform.ui</tt> file containing the form <tt>MyForm</tt> is now converted into a single header file <tt>ui_myform.h</tt>, which contains the declaration and inline definition of a POD class <tt>Ui::MyForm</tt>.</li>
<li><b>New resource file system.</b> Icon data is no longer stored in the <tt>.ui</tt> file. Instead, icons are put into resource files (<tt>.qrc</tt>).</li>
</ul>
<p>The rest of this document explains how to deal with the main differences between Qt Designer 3 and Qt Designer 4:</p>
<ul><li><a href="#uic-output">uic Output</a></li>
<li><a href="#working-with-uic3">Working with uic3</a></li>
<li><a href="#limitations-of-uic3">Limitations of uic3</a></li>
<li><a href="#icons">Icons</a></li>
<li><a href="#custom-widgets">Custom Widgets</a></li>
</ul>
<p>See <a href="porting4.html">Porting to Qt 4</tt></a> and <a href="qt3to4.html"><tt>qt3to4 - The Qt 3 to 4 Porting Tool</tt></a> for more information about porting from Qt 3 to Qt 4. See also the <a href="designer-manual.html">Qt Designer Manual</tt></a>.</p>
<a name="uic-output"></a>
<h2>uic Output</h2>
<p>In Qt 3, <tt>uic</tt> generated a header file and an implementation for a class, which inherited from one of Qt's widgets. To use the form, the programmer included the generated sources into the application and created an instance of the class.</p>
<p>In Qt 4, <tt>uic</tt> creates a header file containing a POD class. The name of this class is the object name of the main container, qualified with the <tt>Ui</tt> namespace (e.g&#x2e;, <tt>Ui::MyForm</tt>). The class is implemented using inline functions, removing the need of a separate <tt>.cpp</tt> file. Just as in Qt 3, this class contains pointers to all the widgets inside the form as public members. In addition, the generated class provides the public method <tt>setupUi()</tt>.</p>
<p>The class generated by <tt>uic</tt> is not a <a href="gui/QWidget.html"><tt>QWidget</tt></a>; in fact, it's not even a <a href="core/QObject.html"><tt>QObject</tt></a>. Instead, it is a class which knows how to populate an instance of a main container with the contents of the form. The programmer creates the main container himself, then passes it to <tt>setupUi()</tt>.</p>
<p>For example, here's the <tt>uic</tt> output for a simple <tt>helloworld.ui</tt> form (some details were removed for simplicity):</p>
<pre>    namespace Ui {

    class HelloWorld
    {
    public:
        QVBoxLayout *vboxLayout;
        QPushButton *pushButton;

        void setupUi(QWidget *HelloWorld)
        {
            HelloWorld-&gt;setObjectName(QString::fromUtf8(&quot;HelloWorld&quot;));

            vboxLayout = new QVBoxLayout(HelloWorld);
            vboxLayout-&gt;setObjectName(QString::fromUtf8(&quot;vboxLayout&quot;));

            pushButton = new QPushButton(HelloWorld);
            pushButton-&gt;setObjectName(QString::fromUtf8(&quot;pushButton&quot;));

            vboxLayout-&gt;addWidget(pushButton);

            retranslateUi(HelloWorld);
        }
    };

    }</pre>
<p>In this case, the main container was specified to be a <a href="gui/QWidget.html"><tt>QWidget</tt></a> (or any subclass of <a href="gui/QWidget.html"><tt>QWidget</tt></a>). Had we started with a <a href="gui/QMainWindow.html"><tt>QMainWindow</tt></a> template in Qt Designer, <tt>setupUi()</tt>'s parameter would be of type <a href="gui/QMainWindow.html"><tt>QMainWindow</tt></a>.</p>
<p>There are two ways to create an instance of our form. One approach is to create an instance of the <tt>Ui::HelloWorld</tt> class, an instance of the main container (a plain <a href="gui/QWidget.html"><tt>QWidget</tt></a>), and call <tt>setupUi()</tt>:</p>
<pre>    #include &lt;QApplication&gt;
    #include &lt;QWidget&gt;

    #include &quot;ui_helloworld.h&quot;   <span class="comment">// defines Ui::HelloWorld</span>

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

        QWidget w;
        Ui::HelloWorld ui;
        ui.setupUi(&amp;w);

        w.show();
        return app.exec();
    }</pre>
<p>The second approach is to inherit from both the <tt>Ui::HelloWorld</tt> class and the main container, and to call <tt>setupUi()</tt> in the constructor of the subclass. In that case, <a href="gui/QWidget.html"><tt>QWidget</tt></a> (or one of its subclasses, e.g&#x2e; <a href="gui/QDialog.html"><tt>QDialog</tt></a>) must appear first in the base class list so that <a href="moc.html#moc">moc</tt></a> picks it up correctly. For example:</p>
<pre>    #include &lt;QApplication&gt;
    #include &lt;QWidget&gt;

    #include &quot;ui_helloworld.h&quot;   <span class="comment">// defines Ui::HelloWorld</span>

    class HelloWorldWidget : public QWidget, public Ui::HelloWorld
    {
        Q_OBJECT

    public:
        HelloWorldWidget(QWidget *parent = 0)
            : QWidget(parent)
        { setupUi(this); }
    };

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        HelloWorldWidget w;
        w.show();
        return app.exec();
    }</pre>
<p>This second method is useful when porting Qt 3 forms to Qt 4. <tt>HelloWorldWidget</tt> is a class whose instance is the actual form and which contains public pointers to all the widgets in it. It therefore has an interface identical to that of a class generated by <tt>uic</tt> in Qt 3.</p>
<p>Creating POD classes from <tt>.ui</tt> files is more flexible and generic than the old approach of creating widgets. Qt Designer doesn't need to know anything about the main container apart from the base widget class it inherits. Indeed, <tt>Ui::HelloWorld</tt> can be used to populate any container that inherits <a href="gui/QWidget.html"><tt>QWidget</tt></a>. Conversely, all non-GUI aspects of the main container may be implemented by the programmer in the application's sources without reference to the form.</p>
<a name="working-with-uic3"></a>
<h2>Working with uic3</h2>
<p>Qt 4 comes with the tool <tt>uic3</tt> for working with old <tt>.ui</tt> files. It can be used in two ways:</p>
<ol type="1">
<li>To generate headers and source code for a widget to implement any custom signals and slots added using Qt Designer 3.</li>
<li>To generate a new <tt>.ui</tt> file that can be used with Qt Designer 4.</li>
</ol>
<p>You can use both these methods in combination to obtain <tt>.ui</tt>, header and source files that you can use as a starting point when porting your user interface to Qt 4.</p>
<p>The first method generates a Qt 3 style header and implementation which uses Qt 4 widgets (this includes the Qt 3 compatibility classes present in the Qt3Support library). This process should be familiar to anyone used to working with Qt Designer 3:</p>
<pre>    uic3 myform.ui &gt; myform.h
    uic3 -impl myform.h myform.ui &gt; myform.cpp</pre>
<p>The resulting files <tt>myform.h</tt> and <tt>myform.cpp</tt> implement the form in Qt 4 using a <a href="gui/QWidget.html"><tt>QWidget</tt></a> that will include custom signals, slots and connections specified in the <tt>.ui</tt> file. However, see below for the <a href="porting4-designer.html#limitations-of-uic3">limitations</tt></a> of this method.</p>
<p>The second method is to use <tt>uic3</tt> to convert a Qt Designer 3 <tt>.ui</tt> file to the Qt Designer 4 format:</p>
<pre>    uic3 -convert myform3.ui &gt; myform4.ui</pre>
<p>The resulting file <tt>myform4.ui</tt> can be edited in Qt Designer 4. The header file for the form is generated by Qt 4's <tt>uic</tt>. See the <a href="designer-using-a-component.html">Using a Component in Your Application</tt></a> chapter of the <a href="designer-manual.html">Qt Designer Manual</tt></a> for information about the preferred ways to use forms created with Qt Designer 4.</p>
<p><tt>uic3</tt> tries very hard to map Qt 3 classes and their properties to Qt 4. However, the behavior of some classes changed significantly in Qt 4. To keep the form working, some Qt 3 classes are mapped to classes in the Qt3Support library. Table 1 shows a list of classes this applies to.</p>
<p><table align="center" cellpadding="2" cellspacing="1" border="0">
<thead><tr valign="top" class="qt-style"><th>Qt 3 class</th><th>Qt 4 class</th></tr></thead>
<tr valign="top" class="odd"><td><tt>QButtonGroup</tt></td><td>Q3ButtonGroup</td></tr>
<tr valign="top" class="even"><td><tt>QDateEdit</tt></td><td>Q3DateEdit</td></tr>
<tr valign="top" class="odd"><td><tt>QDateTimeEdit</tt></td><td>Q3DateTimeEdit</td></tr>
<tr valign="top" class="even"><td><tt>QGroupBox</tt></td><td>Q3GroupBox</td></tr>
<tr valign="top" class="odd"><td><tt>QListBox</tt></td><td>Q3ListBox</td></tr>
<tr valign="top" class="even"><td><tt>QListView</tt></td><td>Q3ListView</td></tr>
<tr valign="top" class="odd"><td><tt>QMainWindow</tt></td><td>Q3MainWindow</td></tr>
<tr valign="top" class="even"><td><tt>QTextEdit</tt></td><td>Q3TextEdit</td></tr>
<tr valign="top" class="odd"><td><tt>QTextView</tt></td><td>Q3TextView</td></tr>
<tr valign="top" class="even"><td><tt>QTimeEdit</tt></td><td>Q3TimeEdit</td></tr>
<tr valign="top" class="odd"><td><tt>QWidgetStack</tt></td><td>Q3WidgetStack</td></tr>
<tr valign="top" class="even"><td><tt>QWizard</tt></td><td>Q3Wizard</td></tr>
</table></p>
<a name="limitations-of-uic3"></a>
<h2>Limitations of uic3</h2>
<p>Converting Qt 3 <tt>.ui</tt> files to Qt 4 has some limitations. The most noticeable limitation is the fact that since <tt>uic</tt> no longer generates a <a href="core/QObject.html"><tt>QObject</tt></a>, it's not possible to define custom signals or slots for the form. Instead, the programmer must define these signals and slots in the main container and connect them to the widgets in the form after calling <tt>setupUi()</tt>. For example:</p>
<pre>    class HelloWorldWidget : public QWidget, public Ui::HelloWorld
    {
        Q_OBJECT

    public:
        HelloWorldWidget(QWidget *parent = 0);

    public slots:
        void mySlot();
    };

    HelloWorldWidget::HelloWorldWidget(QWidget *parent)
        : QWidget(parent)
    {
        setupUi(this);

        QObject::connect(pushButton, SIGNAL(clicked()),
                         this, SLOT(mySlot()));
    }

    void HelloWorldWidget::mySlot()
    {
        ...
    }</pre>
<p>A quick and dirty way to port forms containing custom signals and slots is to generate the code using <tt>uic3</tt>, rather than <tt>uic</tt>. Since <tt>uic3</tt> does generate a <a href="gui/QWidget.html"><tt>QWidget</tt></a>, it will populate it with custom signals, slots and connections specified in the <tt>.ui</tt> file. However, <tt>uic3</tt> can only generate code from Qt 3 <tt>.ui</tt> files, which implies that the <tt>.ui</tt> files never get translated and need to be edited using Qt Designer 3.</p>
<p>Note also that it is possible to create implicit connections between the widgets in a form and the main container. After <tt>setupUi()</tt> populates the main container with child widgets it scans the main container's list of slots for names with the form <tt>on_<i>objectName</i>_<i>signalName</i>().</tt></p>
<p>If the form contains a widget whose object name is <tt><i>objectName</i></tt>, and if that widget has a signal called <tt><i>signalName</i></tt>, then this signal will be connected to the main container's slot. For example:</p>
<pre>    class HelloWorldWidget : public QWidget, public Ui::HelloWorld
    {
        Q_OBJECT

    public:
        HelloWorldWidget(QWidget *parent = 0);

    public slots:
        void on_pushButton_clicked();
    };

    HelloWorldWidget::HelloWorldWidget(QWidget *parent)
        : QWidget(parent)
    {
        setupUi(this);
    }

    void HelloWorldWidget::on_pushButton_clicked()
    {
        ...
    }</pre>
<p>Because of the naming convention, <tt>setupUi()</tt> automatically connects <tt>pushButton</tt>'s <tt>clicked()</tt> signal to <tt>HelloWorldWidget</tt>'s <tt>on_pushButton_clicked()</tt> slot.</p>
<a name="icons"></a>
<h2>Icons</h2>
<p>In Qt 3, the binary data for the icons used by a form was stored in the <tt>.ui</tt> file. In Qt 4 icons and any other external files can be compiled into the application by listing them in a <a href="resources.html">resource file</tt></a> (<tt>.qrc</tt>). This file is translated into a C++ source file using Qt's resource compiler (<tt>rcc</tt>). The data in the files is then available to any Qt class which takes a file name argument.</p>
<p>Imagine that we have two icons, <tt>yes.png</tt> and <tt>no.png</tt>. We create a resource file called <tt>icons.qrc</tt> with the following contents:</p>
<pre>    &lt;RCC version=&quot;1.0&quot;&gt;
        &lt;qresource prefix=&quot;/icons&quot;&gt;
            &lt;file&gt;yes.png&lt;/file&gt;
            &lt;file&gt;no.png&lt;/file&gt;
        &lt;/qresource&gt;
    &lt;/RCC&gt;</pre>
<p>Next, we add the resource file to our <tt>.pro</tt> file:</p>
<pre>    RESOURCES += icons.qrc</pre>
<p>When <tt>qmake</tt> is run, it will create the appropriate Makefile rules to call <tt>rcc</tt> on the resource file, and compile and link the result into the application. The icons may be accessed as follows:</p>
<pre>    QFile file(&quot;:/icons/yes.png&quot;);
    QIcon icon(&quot;:/icons/no.png&quot;);
    QPixmap pixmap(&quot;:/icons/no.png&quot;);</pre>
<p>In each case, the leading colon tells Qt to look for the file in the virtual file tree defined by the set of resource files compiled into the application instead of the file system.</p>
<p>In the <tt>.qrc</tt> file, the <tt>qresource</tt> tag's <tt>prefix</tt> attribute is used to arrange the files into categories and set a virtual path where the files will be accessed.</p>
<p>Caveat: If the resource file was not linked directly into the application, but instead into a dynamic or static library that was later linked with the application, its virtual file tree will not be available to <a href="core/QFile.html"><tt>QFile</tt></a> and friends until the Q_INIT_RESOURCE() macro is called. This macro takes one argument, which is the name of the <tt>.qrc</tt> file, without the path or the file extension. A convenient place to initialize resources is at the top of the application's <tt>main()</tt> function.</p>
<p>In Qt Designer 4, we can associate any number of resource files with a form using the resource editor tool. The widgets in the form can access all icons specified in its associated resource files.</p>
<p>In short, porting of icons from a Qt 3 to a Qt 4 form involves the following steps:</p>
<ol type="1">
<li>Use <tt>uic3 -convert</tt> to obtain a <tt>.ui</tt> file understood by Qt Designer 4.</li>
<li>Create a <tt>.qrc</tt> file with a list of all the icon files.</li>
<li>Add the resource file to the <tt>.pro</tt> file.</li>
<li>Open the form in Qt Designer 4 and add the resource file to the form's resource editor.</li>
<li>Set the icon properties for the appropriate widgets.</li>
</ol>
<a name="custom-widgets"></a>
<h2>Custom Widgets</h2>
<p>Qt Designer 3 supported defining custom widgets by specifying their name, header file and methods. In Qt Designer 4, a custom widget is always created by &quot;promoting&quot; an existing Qt widget to a custom class. Qt Designer 4 assumes that the custom widget will inherit from the widget that has been promoted. In the form editor, the custom widget will retain the looks, behavior, properties, signals and slots of the base widget. It is not currently possible to tell Qt Designer 4 that the custom widget will have additional signals or slots.</p>
<p><tt>uic3 -convert</tt> handles the conversion of custom widgets to the new <tt>.ui</tt> format, however all custom signals and slots are lost. Furthermore, since Qt Designer 3 never knew the base widget class of a custom widget, it is taken to be <a href="gui/QWidget.html"><tt>QWidget</tt></a>. This is often sufficient. If not, the custom widgets have to be inserted manually into the form.</p>
<p>Custom widget plugins, which contain custom widgets to be used in Qt Designer, must themselves be ported before they can be used in forms ported with <tt>uic3</tt>. The <a href="porting4.html">Porting to Qt 4</tt></a> document contains information about general porting issues that may apply to the custom widget code itself, and the <a href="designer-creating-custom-widgets.html">Creating Custom Widgets for Qt Designer</tt></a> chapter of the <a href="designer-manual.html">Qt Designer Manual</tt></a> describes how the ported widget should be built in order to work in Qt Designer 4.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2007 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Jambi </div></td>
</tr></table></div></address></body>
</html>