Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Multiple Inheritance Example

Files:

The Multiple Inheritance Example shows how to use a form created with Qt Designer in an application by subclassing both QWidget and the user interface class, which is Ui::CalculatorForm.

To subclass the calculatorform.ui file and ensure that qmake processes it with the uic, we have to include calculatorform.ui in the .pro file, as shown below:

 SOURCES = calculatorform.cpp main.cpp
 HEADERS = calculatorform.h
 FORMS = calculatorform.ui

When the project is compiled, the uic will generate a corresponding ui_calculatorform.h.

CalculatorForm Definition

In the CalculatorForm definition, we include the ui_calculatorform.h that was generated earlier.

 #include "ui_calculatorform.h"

As mentioned earlier, the class is a subclass of both QWidget and Ui::CalculatorForm.

 class CalculatorForm : public QWidget, private Ui::CalculatorForm
 {
     Q_OBJECT

 public:
     CalculatorForm(QWidget *parent = 0);

 private slots:
     void on_inputSpinBox1_valueChanged(int value);
     void on_inputSpinBox2_valueChanged(int value);
 };

Two slots are defined according to the automatic connection naming convention required by uic. This is to ensure that QMetaObject's auto-connection facilities connect all the signals and slots involved automatically.

CalculatorForm Implementation

In the constructor, we call setupUi() to load the user interface file. Note that we do not need the ui prefix as CalculatorForm is a subclass of the user interface class.

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

We include two slots, on_inputSpinBox1_valueChanged() and on_inputSpinBox2_valueChanged(). These slots respond to the valueChanged() signal that both spin boxes emit. Whenever there is a change in one spin box's value, we take that value and add it to whatever value the other spin box has.

 void CalculatorForm::on_inputSpinBox1_valueChanged(int value)
 {
     outputWidget->setText(QString::number(value + inputSpinBox2->value()));
 }

 void CalculatorForm::on_inputSpinBox2_valueChanged(int value)
 {
     outputWidget->setText(QString::number(value + inputSpinBox1->value()));
 }

main() Function

The main() function instantiates QApplication and CalculatorForm. The calculator object is displayed by invoking the show() function.

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     CalculatorForm calculator;
 #if defined(Q_OS_SYMBIAN)
     calculator.showMaximized();
 #else
     calculator.show();
 #endif
     return app.exec();
 }

There are various approaches to include forms into applications. The Multiple Inheritance approach is just one of them. See Using a Designer UI File in Your Application for more information on the other approaches available.

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.8
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 !
 
 
 
 
Partenaires

Hébergement Web