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

标签 forms qt designer

在 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/

相关文章:

c# - 如何为 InitializeComponent 提供自定义代码?

winforms - Visual Studio 2008 Winform 设计器无法加载从泛型类继承的表单

Ajax 提交表单,即使它没有通过 Bootstrap 验证进行验证

google-apps-script - 从 Google Web App 中的 doPost() 重定向,带有 App 脚本的 HTML 表单

qt - QCompare 的 undefined symbol

c++ - 如何使用TagLib-C++从MP3文件读取XingHeaders,VBRIHeaders和sampleCount

c++ - 如何为自定义 QLabel 实现 setWordWrap(True)?

html - 如何在 D3 中添加 html 表单?

java - struts2 有类似 requireddate 的东西吗?

visual-studio - 预览 Xamarin 表单页面