How to Use the UI File Created by qtdesigner in QT (1)

Source: Internet
Author: User
Tags qt designer
Document directory
  • Reacting to language changes

Qt has been used for some time and has been used in the IDE environment (qtcreator and vs2003 + Integrator), which naturally reduces a lot of trouble steps. However, while enjoying this convenience, we also lose the bit behind understanding more knowledge. In IDE, if we want to develop a dialog box, we usually use "new-> QT designer Interface Class". In this way, IDE will automatically generate three files (filename. UI, filename. h, filename. CPP ). Qmake is also very intelligent. It can automatically detect this user interface file (filename. UI) and produce appropriate makefile rules. In this way, the IDE automatically calls the UIC (User Interface compiler that comes with QT, user
Interface compiler) converts the interface file (filename. UI) into C ++ code and saves it in the ui_filename.h file. And in the other two C ++ source files (filename. H and filename. CPP) uses the class in ui_filename.h in combination (that is, delegate or proxy) (this class is usually defined under the namespace UI ).

If you do not take the initiative to pay attention to these details, you may never understand this, even if you have been using QT for many years, this is what I do. Once, the project team's demand staff dismissed our developers' improper UI layout and made it beautiful. So I had the idea of developing the interface myself. Good! Developers quickly taught the required personnel to design the form interface using QT designer. However, when the required personnel threw the pureui_filename.ui file to our developers for use, we were suddenly dumb-faced. How can we use it? So I used the most stupid method, of course, and the simplest method: like before, I used the IDE "new-> QT designer Interface Class" to generate a file with "pureui_filename. "files with the same name, and then replace the files automatically generated by IDE with the pureui_filename.ui
*. UI file. Although it turns into a small bend, the goal is achieved!

Later, I thought it was a bit regrettable, so I checked the QT document.Using a designer UI file in your application

This document details how to use the UI file in an application.

1. Direct Method (the direct approach)

Filename. the UI is directly included in the UIC-converted C ++ code file ui_filename.h, using the class in the UI namespace (the name is the same as the objectname of the main form, which is assumed to be gotocelldialog ).

# Include "ui_gotocelldialog.h" // UIC tool sets gotocelldialog. UI-generated C ++ code int main (INT argc, char * argv []) {qapplication app (argc, argv); qdialog * dialog = new qdialog; // display the UI of the parent qdialog (subclass of qwidget) UI: gotocelldialog UI; // interface class, which must be displayed on a qwidget (or its subclass) UI. setupui (DIALOG); // set qdialog to the parent form of gotocelldialog, so that the controls defined in gotocelldialog are displayed in the qdialog form dialog-> show (); return app.exe C ();}

II. The single inheritance Approach)

The single inheritance method is compared with the Multi-Inheritance Method described later. The single inheritance method is also called the combination (that is, the delegate or proxy) method. In simple terms, the single inheritance method is to first define a subclass in the Code (for example, the gotocelldialog class in the following section), which should be derived from the form class (or its compatible subclass) corresponding to the form; use the class generated by the UI to define the member variables in a class. The member variables can be values or pointers. They are different in the form of member variables, it can also be divided into member variables and pointer member variables. In this way, variables and functions in the UI and UI can be directly called in the gotocelldialog constructor, which is convenient to use.

1. Use member variables

Ui: gotocelldialog UI; is a member variable of the class gotocelldialog (only inherited from qdialog, a single inheritance. Note that the class provided by the UI file is included in the Name Space named UI, in this way, The namespace of the UI file is separated from the user's code to avoid conflicts between the two.

Header file: gotocelldialog. h

# Include <qdialog> # include "ui_gotocelldialog.h" // The class gotocelldialog: Public qdialog {q_object public: explicit gotocelldialog (qdialog * parent = 0); Private slots: void on_lineedit_textchanged (); Private: Ui: gotocelldialog UI ;};

Implementation file: gotocelldialog. cpp

#include "gotocelldialog.h"#include <QtGui>GoToCellDialog::GoToCellDialog(QWidget *parent)    : QDialog(parent){    ui.setupUi(this);      QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");    lineEdit->setValidator(new QRegExpValidator(regExp, this));    connect(okButton, SIGNAL(clicked()), SLOT(accept()));    connect(cancelButton, SIGNAL(clicked()), SLOT(reject()));}void GoToCellDialog::on_lineEdit_textChanged(){    // boolhasAcceptableInput () const    // This property holds whether the input satisfies the inputMask and the validator.    // By default, this property is true.    okButton->setEnabled(lineEdit->hasAcceptableInput());}

2. Use Pointer member variables
Similar to the form of member variables, the only difference is that the UI: gotocelldialog is declared as a pointer member, that is, the UI: gotocelldialog * UI;

Therefore, only the pre-declaration is required in the corresponding header file:

Namespace UI {class gotocelldialog;} // pre-declaration. Only the implementation file contains the corresponding header file class gotocelldialog: Public qdialog {// same as the above private: Ui :: gotocelldialog * UI ;};

Implementation file:

# Include "Custom" gotocelldialog: gotocelldialog (qdialog * parent): qdialog (parent), UI (new UI: gotocelldialog) {UI-> setupui (this);} calculatorform: :~ Calculatorform () {Delete UI; // remember to delete and release resources}

III. The multiple inheritance Approach)
Multi-inheritance means that custom classes are derived from multiple forms and UI classes. The code is clear:

Header file:

#ifndef GOTOCELLDIALOG_H#define GOTOCELLDIALOG_H#include <QDialog>#include "ui_gotocelldialog.h"class GoToCellDialog :  public QDialog, public Ui::GoToCellDialog{    Q_OBJECTpublic:    explicit GoToCellDialog(QWidget *parent = 0);private slots:    void on_lineEdit_textChanged();};#endif // GOTOCELLDIALOG_H

Implementation file:

# Include "gotocelldialog. H "# include <qtgui> gotocelldialog: gotocelldialog (qwidget * parent): qdialog (parent) {This-> setupui (this); // 1st this indicates the UI :: gotocelldialog, 2nd this indicates (qdialog) that is, UI: gotocelldialog-> setupui (qdialog) qregexp Regexp ("[A-Za-Z] [1-9] [0-9] {0, 2}"); lineedit-> setvalidator (New qregexpvalidator (Regexp, this); Connect (okbutton, signal (clicked (), slot (accept (); Connect (cancelbutton, signal (clicked ()), slot (reject ();} void gotocelldialog: on_lineedit_textchanged () {// boolhasacceptableinput () const/this property holds whether the input satisfies the inputmask and the validator. // by default, this property is true. okbutton-> setenabled (lineedit-> hasacceptableinput ());}

PS: About internationalization in the UI

If the user interface language sends a change, QT notifies the application by sending the qevent: languagechange event. To call the retranslateui () function to switch the language, we need to re-implement the qwidget: changeevent () event processing function in the interface class.

Reacting to language changes

Qt notifies applications if the user interface language changes by sending an event of the type qevent: languagechange. To call the Member
FunctionRetranslateui ()Of the user interface object, we reimplementQwidget: changeevent ()In the form class, as follows:

 

 void CalculatorForm::changeEvent(QEvent *e) {     QWidget::changeEvent(e);     switch (e->type()) {     case QEvent::LanguageChange:         ui->retranslateUi(this);         break;     default:         break;    } }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.