Porting .ui Files to Qt 4 |
Qt 3 class | Qt 4 class |
---|---|
QButtonGroup | Q3ButtonGroup |
QDateEdit | Q3DateEdit |
QDateTimeEdit | Q3DateTimeEdit |
QGroupBox | Q3GroupBox |
QListBox | Q3ListBox |
QListView | Q3ListView |
QMainWindow | Q3MainWindow |
QTextEdit | Q3TextEdit |
QTextView | Q3TextView |
QTimeEdit | Q3TimeEdit |
QWidgetStack | Q3WidgetStack |
QWizard | Q3Wizard |
Converting Qt 3 .ui files to Qt 4 has some limitations. The most noticeable limitation is the fact that since uic no longer generates a QObject, 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 setupUi(). For example:
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() { ... }
A quick and dirty way to port forms containing custom signals and slots is to generate the code using uic3, rather than uic. Since uic3 does generate a QWidget, it will populate it with custom signals, slots and connections specified in the .ui file. However, uic3 can only generate code from Qt 3 .ui files, which implies that the .ui files never get translated and need to be edited using Qt Designer 3.
Note also that it is possible to create implicit connections between the widgets in a form and the main container. After setupUi() populates the main container with child widgets it scans the main container's list of slots for names with the form on_objectName_signalName().
If the form contains a widget whose object name is objectName, and if that widget has a signal called signalName, then this signal will be connected to the main container's slot. For example:
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() { ... }
Because of the naming convention, setupUi() automatically connects pushButton's clicked() signal to HelloWorldWidget's on_pushButton_clicked() slot.
In Qt 3, the binary data for the icons used by a form was stored in the .ui file. In Qt 4 icons and any other external files can be compiled into the application by listing them in a resource file (.qrc). This file is translated into a C++ source file using Qt's resource compiler (rcc). The data in the files is then available to any Qt class which takes a file name argument.
Imagine that we have two icons, yes.png and no.png. We create a resource file called icons.qrc with the following contents:
<RCC version="1.0"> <qresource prefix="/icons"> <file>yes.png</file> <file>no.png</file> </qresource> </RCC>
Next, we add the resource file to our .pro file:
RESOURCES += icons.qrc
When qmake is run, it will create the appropriate Makefile rules to call rcc on the resource file, and compile and link the result into the application. The icons may be accessed as follows:
QFile file(":/icons/yes.png"); QIcon icon(":/icons/no.png"); QPixmap pixmap(":/icons/no.png");
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.
In the .qrc file, the qresource tag's prefix attribute is used to arrange the files into categories and set a virtual path where the files will be accessed.
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 QFile and friends until the Q_INIT_RESOURCE() macro is called. This macro takes one argument, which is the name of the .qrc file, without the path or the file extension. A convenient place to initialize resources is at the top of the application's main() function.
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.
In short, porting of icons from a Qt 3 to a Qt 4 form involves the following steps:
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 "promoting" 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.
uic3 -convert handles the conversion of custom widgets to the new .ui 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 QWidget. This is often sufficient. If not, the custom widgets have to be inserted manually into the form.
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 uic3. The Porting to Qt 4 document contains information about general porting issues that may apply to the custom widget code itself, and the Creating Custom Widgets for Qt Designer chapter of the Qt Designer Manual describes how the ported widget should be built in order to work in Qt Designer 4.
[Previous: Porting to Qt 4 - Drag and Drop] [Contents] [Next: Porting to Graphics View]
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.4 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com