Friday 15 March 2013

c++ - Inheriting a MainWindow class from QMainWindow and Ui_MainWindow -


I'm learning QT, QT5. When I start the Qt Creator and prepare a project with all the default settings, I get these 2 files, (I'm leaving the main CPP and .pro files)

mainwindow.h

  #ifndef MAINWINDOW_H # defined MAINWINDOW_H # included & lt; QMainWindow & gt; Namespace UI {Class MainWindow; } Class Main Window: Public QMainWindow {Q_OBJECT Public: Clear Men Window (QWidget * parent = 0); ~ MainWindow (); Private: UI :: MainWondo * UI; }; #endif // MAINWINDOW_H   

mainwindow.cpp

 code = #include "mainwindow.h" #include "ui_mainwindow .h "MainWindows :: Men Wando (Quedgate * Parent): Q Menine (UI) (New UI: Main Wando) (UI-> Setup UI (this);} Main Window :: ~ MAIN WANDO    

Now, I like to do it like this,

My_mainwindow.h

  #ifndef MAINWINDOW_H # include MAINWINDOW_H # include & lt; QMainWindow & gt; #include "ui_mainwindow.h" class MainWindow: public QMainWindow, Private Ui_MainWindow {Q_OBJECT Public: Clear MainWindow (QWidget * parent = 0);}; #endif // MAINWINDOW_H   

my_mainwindow.cpp


The main differences between the code of my code and the QT creator are:

  1. My In the code, Nameshot Yue no. (Can someone explain me the use of this namespace here?)
  2. I use both the MainWindow class both QMainWindow and Ui_MainWindow > While the code of Qt Creator gets it from the QMainWindow class only.

    My question is whether there is no harm to using my approach or is there any use of it? Qt is using the manufacturer's approach? Please give a detailed answer.

    1. An advantage of namespace is that this naming conflict prevents all of the QtDesigner automatically Generated names live in their own namespace.
    2. There is an advantage of making the UI class a member, rather than using many heritage, it is only that the Ui class should be forwarded only to the header. In your code, you have a #include "ui_mainwindow.h" , and in return, many coolouts like & lt; QLabel & gt; , & lt; QPushButton & gt; etc. This reduces compilation speed significantly, which includes mainwindow.h , all of which include those QtWidgets when Ui class is used as a member And it is forwarded, then in all these, only need to be compiled when compiling mainwindow.cpp , but since mainwindow.h does not include Goes somewhere else

No comments:

Post a Comment