Qt GUI 对话框初始化混乱
我正在学习使用 Qt 进行 GUI 编码,希望能消除我的一些困惑。当我使用 Qt Creator 创建一个对话框时,它会为我创建这样的代码...
#ifndef LISTDIALOG_H
#define LISTDIALOG_H
#include <QDialog>
#include "ui_listdialog.h" //Q1:Why was this auto paced in cpp file instead of h file?
//Q2: This is what I'm really confused about.
//Is Ui namespace wrapping ui_listdialog class or the ListDialog class?
namespace Ui {
class ListDialog;
}
class ListDialog : public QDialog
{
Q_OBJECT
public:
explicit ListDialog(QWidget *parent = 0); //Q3: Why is this constructor explicit?
~ListDialog();
//CUSTOM FUNCTIONALITY NOT ADDED BY CREATOR (IGNORE FOR MY POST)
private slots:
void addItem();
void editItem();
void deleteItem();
//END CUSTOM FUNCTIONALITY
private:
Ui::ListDialog *ui; //Placed on heap instead of stack.
};
#endif // LISTDIALOG_H
上面的代码中的某些内容与我的 3 本书不同(所有 3 本书都已过时并忽略 Creator)。
我的主要困惑来自第二季度。 “Ui”是包装“ui_listdialog.h”还是我在这里发布的类(类 ListDialog )?对我来说,语法似乎暗示它正在包装后者,但我觉得它实际上必须包装 ui_listdialog.h 类。对此非常困惑。有人可以清楚地解释这一点吗?
我也不明白为什么 Creator 显式地构造了构造函数。我在三本书中都没有看到这一点。
I am learning GUI coding with Qt and hope to clear up some confusion on my part. When I create a dialog with Qt Creator it creates code for me like this...
#ifndef LISTDIALOG_H
#define LISTDIALOG_H
#include <QDialog>
#include "ui_listdialog.h" //Q1:Why was this auto paced in cpp file instead of h file?
//Q2: This is what I'm really confused about.
//Is Ui namespace wrapping ui_listdialog class or the ListDialog class?
namespace Ui {
class ListDialog;
}
class ListDialog : public QDialog
{
Q_OBJECT
public:
explicit ListDialog(QWidget *parent = 0); //Q3: Why is this constructor explicit?
~ListDialog();
//CUSTOM FUNCTIONALITY NOT ADDED BY CREATOR (IGNORE FOR MY POST)
private slots:
void addItem();
void editItem();
void deleteItem();
//END CUSTOM FUNCTIONALITY
private:
Ui::ListDialog *ui; //Placed on heap instead of stack.
};
#endif // LISTDIALOG_H
There are things in the above code that differ from my 3 Qt books (all 3 out of date and ignore Creator).
My main confusion comes from Q2. Is "Ui" wrapping "ui_listdialog.h" or the class I have posted here ( class ListDialog )? The syntax seems to imply to me that it is wrapping the latter but I feel it must be actually wrapping the ui_listdialog.h class instead. Very confused about this. Can someone explain this clearly?
I also don't understand why the constructor was made explicit by Creator. I have not seen that in any of the 3 books.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Q1. #include 放置在 .cpp 中以避免头文件中存在太多依赖项。这会缩短编译时间,因为如果您更改对话框,您唯一需要重新编译的是 .cpp,而不是包含头文件的所有内容。一般来说,如果类的前向声明就足够了(即类中只有一个指针或对它的引用),那么最好不要 #include 类的定义。
Q2。 Ui 是一个命名空间,其中包含一个名为 ListDialog 的不同类。您可以打开头文件并查看另一个类的定义。在你习惯之前有点混乱。
Q3。在带有单个参数的构造函数中使用
explicit
关键字是一个好习惯。否则,构造函数也可以用作自动转换运算符,如果您不知道的话,这可能会导致问题。例如,如果您有一个采用 ListDialog 参数的函数,并且传递了 QWidget * 参数,则它可能会调用构造函数,而实际上您希望编译器喊叫(无效参数)。Q1. The #include is placed in the .cpp to avoid too many dependencies in the header file. This shortens compilation time, because if you change the dialog, the only thing you have to recompile is the .cpp and not everything that includes your header file. In general, if a forward declaration of a class is enough (i.e. you only have a pointer or a reference to it in your class), then it's better not to #include the class's definition.
Q2. Ui is a namespace that contains a different class called ListDialog. You can open the header file and see the definition of this other class. A bit confusing until you get used to it.
Q3. It's a good habit to use the
explicit
keyword with constructors that take a single parameter. Otherwise the constructor can also be used as an automatic conversion operator, and this can cause problems if you're not aware of it. For example, if you have a function that takes a ListDialog parameter, and you pass a QWidget * parameter, it may call the constructor when in fact you want the compiler to shout (invalid parameter).ui_listdialog.h
包含基于 Qt Designer 生成用户界面的实现。声明类时没有必要 - 这就是文件在.cpp
中#include
的原因(Q1)。如果标头中没有ui_listdialog.h
,则需要类声明(Q2)。至于第三个问题,它可能是为了让你使用构造函数语法。否则,您可能会写出误导性的陈述,例如
The
ui_listdialog.h
contains implementation to generate your user interface based on Qt Designer. It isn't necessary when declaring the class -- that's why the file was#include
d in the.cpp
(Q1). Without theui_listdialog.h
in the header, the class declaration is necessary (Q2).As for Q3, it's probably there to make you use the constructor syntax. Else, you could write misleading statements like