I am learning Qt, Qt 5. When I start Qt Creator and create a project with ALL the default settings, I get these 2 files generated, (I am excluding main.cpp and the .pro file)
我正在学习Qt, Qt 5。当我启动Qt创建者并创建一个包含所有默认设置的项目时,我将生成这两个文件(我不包括main)。cpp和。pro文件
mainwindow.h
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
Now, I prefer to do it this way,
现在,我更喜欢这样做,
my_mainwindow.h
my_mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
class MainWindow : public QMainWindow, private Ui_MainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
};
#endif // MAINWINDOW_H
my_mainwindow.cpp
my_mainwindow.cpp
#include "my_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setupUi(this);
}
MainWindow::~MainWindow()
{
}
Here are the main differences between my code and Qt Creator's code:
以下是我的代码和Qt创建者的代码之间的主要区别:
- No
namespace Ui
in my code. (Could anyone explain me the use of this namespace here ?) - 我的代码中没有名称空间Ui。(有人能解释一下这个名称空间的用法吗?)
- I inherit the
MainWindow
class from bothQMainWindow
andUi_MainWindow
whereas the Qt Creator's code inherits it only from theQMainWindow
class. - 我从QMainWindow和Ui_MainWindow继承了MainWindow类,而Qt创建者的代码仅从QMainWindow类继承它。
My question is, is there ANY disadvantage of using my approach or is there any advantage of using the Qt Creator's approach ? Please give a detailed answer.
我的问题是,使用我的方法有什么缺点吗?或者使用Qt创建者的方法有什么好处?请给出详细的答案。
1 个解决方案
#1
4
- An advantage of the namespace is that it prevents naming *es. All the automatically generated names from QtDesigner stay in their own namespace.
- 命名空间的优点是它可以防止命名冲突。所有来自QtDesigner的自动生成的名称都保留在它们自己的名称空间中。
- An advantage of making the Ui class a member, instead of using multiple inheritance, is that the Ui class only needs to be forward-declarded in the header. In your code, you have a
#include "ui_mainwindow.h"
, and that in turn drags in a lot of includes from QtWidgets like<QLabel>
,<QPushButton>
etc. This decreases compilation speed significantly, as everyone who includesmainwindow.h
now also includes those QtWidgets includes. When using the Ui class as a member and forward-declaring it, all these includes only need to be compiled when compilingmainwindow.cpp
, but not when includingmainwindow.h
from somewhere else. -
使Ui类成为成员而不是使用多重继承的优点是,Ui类只需要在头中声明。在您的代码中,有一个#include“ui_mainwindow”。h“,而这又会使很多包括qtwidget(比如
)、 等等)的内容减少,这大大降低了编译速度,因为每个人都包括mainwindow。h现在还包含了那些QtWidgets。当使用Ui类作为成员并向前声明时,所有这些都只需要在编译主窗口时编译。cpp,但不包括主窗口。从别的地方h。
#1
4
- An advantage of the namespace is that it prevents naming *es. All the automatically generated names from QtDesigner stay in their own namespace.
- 命名空间的优点是它可以防止命名冲突。所有来自QtDesigner的自动生成的名称都保留在它们自己的名称空间中。
- An advantage of making the Ui class a member, instead of using multiple inheritance, is that the Ui class only needs to be forward-declarded in the header. In your code, you have a
#include "ui_mainwindow.h"
, and that in turn drags in a lot of includes from QtWidgets like<QLabel>
,<QPushButton>
etc. This decreases compilation speed significantly, as everyone who includesmainwindow.h
now also includes those QtWidgets includes. When using the Ui class as a member and forward-declaring it, all these includes only need to be compiled when compilingmainwindow.cpp
, but not when includingmainwindow.h
from somewhere else. -
使Ui类成为成员而不是使用多重继承的优点是,Ui类只需要在头中声明。在您的代码中,有一个#include“ui_mainwindow”。h“,而这又会使很多包括qtwidget(比如
)、 等等)的内容减少,这大大降低了编译速度,因为每个人都包括mainwindow。h现在还包含了那些QtWidgets。当使用Ui类作为成员并向前声明时,所有这些都只需要在编译主窗口时编译。cpp,但不包括主窗口。从别的地方h。