Use of qt ui in programs

Source: Internet
Author: User
Tags qt designer

Original works of Li Wanpeng, majoring in software engineering, Harbin Institute of Technology

Http://blog.csdn.net/woshixingaaa/archive/2010/08/09/5798356.aspx

First, we will introduce the UI file. QT can generate the UI file through QT designer. The UI file records the interface content generated by the designer in XML format (the general content includes the properties of widgets, related attributes of the content, header files, variables, slots, and functions ). The form (ui file) created by using designer is finally converted to C ++ during qmake.Code. Qmake can detect form files (*. UI files ),
Using UIC (User Interface compiler), you can translate the content of the UI file into a standard. h and. cpp file, and save it in the ui_xxx.h file. This file provides a complete definition of the corresponding dialog class. The class name is ui_dialog and contains a setupui () member function for initial form conversion. At the end of the header file, use a UI namespace to include the ui_dialog class and derive a subclass known as dialog, while UI :: the dialog interface becomes an external interface designed by the designer.

Use designer to generate the UI in three ways

1. Direct use

2. Single Inheritance Law

3. Multi-Inheritance Law

I. Let's look at a direct example:

Qwidget * w = new qwidget; <br/> UI: Small S; <br/> S. setupui (w); <br/> W. Show ();

Ii. Discuss the ticket inheritance law now:

Use this instance
Designer
Generate three simple UIS. during use, the two UIS are inserted to the masterProgramIn qtabwidget, another UI is triggered by a button,

Use designer to implement three UIS

 

// Widget. h <br/> # ifndef widget_h <br/> # define widget_h <br/> # include <qtabwidget> <br/> # include <qwidget> <br/> # include <qtgui> <br/> # include "ui_first.h" // header file containing three UIS <br/> # include "ui_second.h" <br/> # include "ui_third.h" <br/> class mywidget: public qwidget {<br/> q_object <br/> Public: <br/> mywidget (); <br/> ~ Mywidget (); <br/> PRIVATE: <br/> qtabwidget * tab; <br/> UI: First firstui; // declare three private form objects in the mywidget class. <br/> UI: Second secondui; // The Code uses this object to operate the form Element <br/> UI: Third thirdui; <br/> private slots: <br/> void slotchild (); <br/>}; <br/> # endif <br/> // widget. CPP <br/> # include "widget. H "<br/> mywidget: mywidget () {<br/> tab = new qtabwidget (this); <br/> qwidget * W1 = new qwidget; // create a qwidget object to use this qwidget The object is a parameter <br/> firstui. setupui (W1); // call setupui () of the First UI to generate the first UI <br/> qwidget * W2 = new qwidget; <br/> secondui. setupui (W2); <br/> tab-> addtab (W1, "first"); <br/> tab-> addtab (W2, "second "); <br/> tab-> resize (300,300); <br/> connect (firstui. childpushbutton, signal (clicked (), this, slot (slotchild (); <br/> connect (secondui. closepushbutton, signal (clicked (), this, slot (close (); <br/>}< br/> mywidget ::~ Mywidget () {<br/>}< br/> void mywidget: slotchild () {<br/> qdialog * W3 = new qdialog; <br/> thirdui. setupui (W3); <br/> W3-> exec (); <br/>}

Note: when using the controls on the UI page, remember to add the UI prefix.

// Main. CPP <br/> # include <qapplication> <br/> # include "widget. H "<br/> int main (INT argc, char * argv []) {<br/> qapplication app (argc, argv); <br/> mywidget S; <br/> S. show (); <br/> return app.exe C (); <br/>}

Similarly, this instance uses multi-inheritance, which requires much complexity. A class is implemented for each page. Here, the third UI dialog box is used as an example,

Implement it in the form of multi-inheritance.

# Include "ui_third.h" <br/> class thirddialog: Public qdialog, private UI: Third {<br/> thirddialog (); <br/> ...... <br/>}< br/> thirddialog: thirddialog () {<br/> setupui (this); <br/>}< br/>

Because thirddialog is a subclass of UI: Third, you can directly call the setupui () function in the constructor to display the third dialog box.

Void mywidget: slotchild () {<br/> thirddialog * DLG = new thirddialog; <br/> DLG-> show (); <br/>}

From above
Pair
The analysis of the two inheritance methods is visible. Multiple inheritance methods can be used to directly call controls or functions on the UI page.

While using the single inheritance method, you need to add the UI object prefix when operating the controls on the UI page, which is troublesome to write code, but for the program

There are many UI pages required in, and the use of the single inheritance method is much simpler and more flexible.

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.