The principle and implementation of UI namespaces and SETUPUI functions in qt < turn >

Source: Internet
Author: User

Using the latest qtcreator to select GUI applications will result in projects containing the following files


Below is a simple analysis of the functions of the following sections.

The. Pro file is for qmake use, not the focus of this article "but it is also very simple", in this not more than repeat.

So, starting with main,

[CPP]View Plaincopyprint?
    1. #include <QtGui/QApplication>
    2. #include "Mainwindow.h"
    3. int main (int argc, char *argv[])
    4. {
    5. Qapplication A (argc, argv);
    6. MainWindow W;
    7. W.show ();
    8. return A.exec ();
    9. }

It's a simple look.

Qapplication A (argc, argv) and a.exec () can be understood to be loaded into the QT architecture, running QT program to have this department, not much to say.

Which called a MainWindow and show it out, the specific analysis under

Here's what's in Mainwindow.h

[CPP]View Plaincopyprint?
  1. #ifndef Mainwindow_h
  2. #define Mainwindow_h
  3. #include <QtGui/QMainWindow>
  4. Namespace Ui
  5. {
  6. Class MainWindow;
  7. }
  8. Class MainWindow: Public Qmainwindow
  9. {
  10. Q_object
  11. Public
  12. MainWindow (Qwidget *parent = 0);
  13. ~mainwindow ();
  14. Private
  15. Ui::mainwindow *ui;
  16. };
  17. #endif//Mainwindow_h


The beginning of the namespace UI may be a bit confusing, because QT separates the UI-related classes independently, but the class name is the same, disabling the namespace difference "but in terms of the current use, I feel this is not good, I will explain the reason behind"

Declaring the namespace UI is because you want to invoke MainWindow in the UI, this MainWindow is not MainWindow, and the *ui pointer involved will call it!

As for Q_object, the classes associated with signal and slots in QT are declared.

Carefully see the structure, the destruction is nothing, only then a *ui! But now, if run, it will only generate a form.

The constructors and destructors are, in fact, mainwindow.c.

[CPP]View Plaincopyprint?
    1. #include "Mainwindow.h"
    2. #include "Ui_mainwindow.h"
    3. Mainwindow::mainwindow (Qwidget *parent)
    4. : Qmainwindow (parent), UI (new Ui::mainwindow)
    5. {
    6. UI->SETUPUI (this);
    7. }
    8. Mainwindow::~mainwindow ()
    9. {
    10. Delete UI;
    11. }

Constructs a new UI domain in the heap on the MainWindow, and calls Setupui, the destructor is just delete it, or very simple!

As mentioned earlier, QT separates the UI very well, and the. ui file in the previous diagram is the layout file that makes Qtdesigner!

Now run, will generate UI_MAINWINDOW.H, which will be related to the real layout of the function, that is, the UI domain of the MainWindow. Look at the following,

[CPP]View Plaincopyprint?
  1. #ifndef Ui_mainwindow_h
  2. #define Ui_mainwindow_h
  3. #include <QtCore/QVariant>
  4. #include <QtGui/QAction>
  5. #include <QtGui/QApplication>
  6. #include <QtGui/QButtonGroup>
  7. #include <QtGui/QHeaderView>
  8. #include <QtGui/QMainWindow>
  9. #include <QtGui/QMenuBar>
  10. #include <QtGui/QStatusBar>
  11. #include <QtGui/QToolBar>
  12. #include <QtGui/QWidget>
  13. Qt_begin_namespace
  14. Class Ui_mainwindow
  15. {
  16. Public
  17. Qmenubar *menubar;
  18. Qtoolbar *maintoolbar;
  19. Qwidget *centralwidget;
  20. Qstatusbar *statusbar;
  21. void Setupui (Qmainwindow *mainwindow)
  22. {
  23. if (Mainwindow->objectname (). IsEmpty ())
  24. Mainwindow->setobjectname (Qstring::fromutf8 ("MainWindow"));
  25. Mainwindow->resize (600, 400);
  26. MenuBar = new Qmenubar (MainWindow);
  27. Menubar->setobjectname (Qstring::fromutf8 ("MenuBar"));
  28. Mainwindow->setmenubar (MenuBar);
  29. Maintoolbar = new Qtoolbar (MainWindow);
  30. Maintoolbar->setobjectname (Qstring::fromutf8 ("Maintoolbar"));
  31. Mainwindow->addtoolbar (Maintoolbar);
  32. Centralwidget = new Qwidget (MainWindow);
  33. Centralwidget->setobjectname (Qstring::fromutf8 ("Centralwidget"));
  34. Mainwindow->setcentralwidget (Centralwidget);
  35. StatusBar = new Qstatusbar (MainWindow);
  36. Statusbar->setobjectname (Qstring::fromutf8 ("StatusBar"));
  37. Mainwindow->setstatusbar (StatusBar);
  38. Retranslateui (MainWindow);
  39. Qmetaobject::connectslotsbyname (MainWindow);
  40. } //Setupui
  41. void Retranslateui (Qmainwindow *mainwindow)
  42. {
  43. Mainwindow->setwindowtitle (Qapplication::translate ("MainWindow", "MainWindow", 0, Qapplication::  UNICODEUTF8));
  44. Q_unused (MainWindow);
  45. } //Retranslateui
  46. };
  47. Namespace Ui {
  48. Class MainWindow: Public Ui_mainwindow {};
  49. } //namespace Ui
  50. Qt_end_namespace
  51. #endif//Ui_mainwindow_h

Roar, all of a sudden a lot more, but in fact, it is very easy. Ui_mainwindow declared a few components, specifically I will not say, because there is nothing to say, it implements the SETUPUI function, that is, the MainWindow in front of the call Setupui.

However, it is important to note that the Qmetaobject::connectslotsbyname function automatically connects the signal and slot of the corresponding name, but notice that it is connected to the incoming MainWindow and its sub-members "not subclasses", notice that the front ui->setupui ( This is the MainWindow in the non-UI domain, so if you want to declare signal and slots, declare them in the MainWindow of the non-UI domain, and then interact with the GUI in the form of ui->xxx! If we drag and drop a button in the Qtdesiner and then click the go to slot, it's easy to verify that.

Retranslateui will be named for the widget in the UI, not to mention it in more detail.

Finally, let's look at this piece of code

[CPP]View Plaincopyprint?
    1. Namespace Ui {
    2. Class MainWindow: Public Ui_mainwindow {};
    3. } //namespace Ui

The *ui of MainWindow in the front non-UI domain points to the MainWindow in the UI domain, and the MainWindow in the UI domain is out of the Ui_mainwindow, and the interior is destitute! "A bit around the mouth."

Take a picture, and then review it.

Finally, there are two points, the personal feeling is the qtcreator of the bug,

One is that if you customize the control, and want to load in the built-in designer, win under the MinGW is not feasible, because the designer in the SDK Suite is compiled with Microsoft compiler, of course, there is a more convenient solution, is to put qtcreator source down, Compile it again with the existing creator, and then overwrite the past.

The second is also mentioned earlier, two the same name of the MainWindow only with the UI domain to distinguish, although feel this is very beautiful from the design, but debugging there will be some problems, in short, in creator debugging does not recognize the correct domain, see example


Like this picture above this should actually point to the non- UI domain of the MainWindow "this is actually pointing to the MainWindow, it does not know which domain is the MainWindow, and then expand on the wrong point to the UI domain", But the debug data area points to the UI domain of MainWindow, of course, is not the solution, you can manually change the UI domain MainWindow the name of the correct debugging information can be obtained, but this is slightly cumbersome, and again run Qmake may be re-modified.

Transferred from: http://blog.csdn.net/songjinshi/article/details/7333119

The principle and implementation of UI namespaces and SETUPUI functions in qt < turn >

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.