在 QT 中创建带有表单的控件时的递归构造函数调用
Recursive Constructor Invocation when creating a Widget with form in QT?
我试图理解为什么在使用表单创建QWidget时自动生成QT。
这是QT生成的代码:
**********************************************************************
* widget.h
**********************************************************************
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#endif // WIDGET_H
**********************************************************************
* widget.cpp
**********************************************************************
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
**********************************************************************
* main.cpp
**********************************************************************
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
我的问题是构造函数中的这条语句:
ui(new ui::Widget)
这是在调用类 Widget 的构造函数时创建一个新的 Widget。这就像递归不是吗?为什么不坏?
它不是递归的,它是ui_Widget.h类的实例,因为您一次
只需要GUI的一个实例这种方法的优点是用户界面对象可以向前声明,这意味着我们不必在标头中包含生成的 ui_Widget.h 文件。然后,可以更改表单,而无需重新编译依赖源文件。如果类受到二进制兼容性限制,这一点尤其重要,因为如您所见,"#include"ui_widget.h"在 cpp a 内不在头文件中
NO。由于命名空间,有两个具有相同名称的不同类:
ui::小部件
和
控件
Ui::Widget 是表单对象的类。
相关文章:
- 通过递归进行因子分解
- 递归函数计算序列中的平方和(并输出过程)
- 使用递归的数组的最小值.这是怎么回事
- 递归列出所有目录中的C++与Python与Ruby的性能
- 递归计数给定目录的文件和所有目录
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在BST的这个简单递归实现中消除警告
- C++:正在检查LinkedList中的回文-递归方法-错误
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 递归无序映射
- TSP递归解的迭代形式
- 如何在Elixir中调用递归函数并行
- 返回递归调用和仅递归调用的区别
- 数组元素打印的递归方法
- 使用递归时获取变量的奇怪值
- 如何在C++中递归地按相反顺序打印集合
- 到连接组件算法的问题(递归)
- 如何使用递归打印修改后的星号三角形图案
- 在 QT 中创建带有表单的控件时的递归构造函数调用
- 递归是一件坏事吗?在递归似乎最有用的地方,是否有更清晰或更有效的方法来解决问题?