gpt4 book ai didi

forms - Qt - 没有类的显示设计器表单

转载 作者:行者123 更新时间:2023-12-02 08:45:33 25 4
gpt4 key购买 nike

在 Qt 中,我创建了一个没有类的设计器窗体。所以基本上,我只有一个文件 myform.ui。我应该写什么代码来显示表单?

最佳答案

如果您在 FORMS 部分的 .pro 中包含 (d) u​​i 文件,则在构建过程中将生成一个特殊的头文件。包含此头文件并使用它在运行时将子窗口小部件添加到您想要的任何 QWidget。

本例中的 ui 文件名为 mywidget.ui。在您的 .pro 文件中,有一行内容是

FORMS += mywidget.ui

QtCreator 将在项目资源管理器中显示该文件。这一步很重要,否则在构建项目时不会生成头文件!

生成的头文件称为 ui_mywidget.h,组成设计窗口的类称为 Ui::MyWidget,可以按如下方式使用。

解决方案 1(QtCreator 在您创建新的“Qt Designer Form Class”时建议的方式):

namespace Ui {
class MyWidget;
}

class MyWidget : public QWidget
{
Q_OBJECT

public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();

private:
Ui::MyWidget *ui; // Pointer to the UI class where the child widgets are
};

#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this); // Create and add the child widgets to this widget
}

MyWidget::~MyWidget()
{
delete ui;
}

然后这个小部件就可以使用了,并且在实例化它时将包含您在设计器中创建的子小部件:

MyWidget widget;
widget.show();

方案二(不继承QWidget):

#include "ui_mywidget.h"
...
QWidget *widget = new QWidget(...);
Ui::MyWidget ui; // Instance of the UI class where the child widgets are
ui.setupUi(widget); // Create and add the child widgets to this widget
widget->show();
...

关于forms - Qt - 没有类的显示设计器表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12750473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com