gpt4 book ai didi

c++ - 显示在 QtDesigner 中创建的 QDialog

转载 作者:行者123 更新时间:2023-11-30 04:02:10 25 4
gpt4 key购买 nike

我使用 Qt Designer 在 MainWindow 旁边创建了第二个表单作为 QDialog。我的问题是如何通过单击 MainWindow 中的按钮来显示此对话框。如果我使用以下代码,它会创建一个 newDialog,但我想使用我在 Qt Designer 中创建的表单。如何嵌入它?

QDialog *myDialog = new QDialog;
myDialog->show();

最佳答案

首先,您需要在 QtDesigner 中创建一个新对话框。在该对话框中添加您需要的所有内容,然后保存所有更改。您将创建的文件将是 .ui。将此文件添加到您的项目中。

在此之后创建一个头文件和cpp文件,并使用您想要的名称(如果您使用与de ui文件相同的名称会更好)。然后在 .h 中:

namespace Ui {
class QDialogExample;
}

class QDialogExample : public QDialog
{
Q_OBJECT

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

private:
Ui::QDialogExample *ui; // This will be the acces to the widgets defined in .ui
};

然后在.cpp文件中:

QDialogExample::QDialogExample(QWidget *parent) :
QDialog(parent),
ui(new Ui::QDialogExample)
{
ui->setupUi(this); // This will init all the widgets
}

然后在 QAction 的插槽或您的自定义按钮中添加对

的调用
QDialogExample pDialogExample = new pDialogExample( this );
pDialogExample->show();

无论如何,要了解它是如何工作的,建议使用 QtCreator 的所有过程。 ,这对创建对话框和您需要的所有小部件非常有帮助。

关于c++ - 显示在 QtDesigner 中创建的 QDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25342595/

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