简体   繁体   English

如何关闭在 Qt 中使用不同 ui 文件的 QDialog?

[英]How to close a QDialog that uses a different ui file in Qt?

I've been trying to close a QDialog that uses a separate ui file.我一直在尝试关闭使用单独的 ui 文件的 QDialog。 The dialog is used in a slot of a different class (TaskManager).该对话框用于不同类(TaskManager)的插槽中。 Sorry for the question but I couldn't find a workaround anywhere.抱歉这个问题,但我在任何地方都找不到解决方法。

I'm creating a ToDo App as a University project (first year, first time using C++ and Qt): as the user clicks on the "Add Task" button in the TaskManager, the dialog is shown.我正在创建一个 ToDo App 作为大学项目(第一年,第一次使用 C++ 和 Qt):当用户单击 TaskManager 中的“添加任务”按钮时,将显示对话框。 The user inserts the tasks attributes in the dialog and then I take that data to create a Task object (different class) that is shown in the TaskManager (works fine if I close the dialog manually).用户在对话框中插入任务属性,然后我使用该数据创建一个 Task 对象(不同的类),该对象显示在 TaskManager 中(如果我手动关闭对话框,则工作正常)。

This is a code snippet of the creation of the dialog:这是创建对话框的代码片段:

void TaskManager::on_pushButton_addTask_clicked()
{
    Ui::AddTask addTaskDialog; // addTaskDialog takes the Ui of the file AddTask.ui
    QDialog dialog;
    addTaskDialog.setupUi(&dialog);
    dialog.exec(); // shows the dialog
    [More Code]
}

I wanted to close the QDialog when the user clicks on QDialogButtonBox:当用户单击 QDialogBu​​ttonBox 时,我想关闭 QDialog:

  • ok -> closes the dialog确定 -> 关闭对话框
  • cancel -> closes and deletes the dialog. cancel -> 关闭并删除对话框。

I have tried something like this, but it doesn't work (I get this build issue: "No matching member function for call to 'connect'):我已经尝试过类似的方法,但它不起作用(我遇到了这个构建问题:“没有匹配的成员函数调用'connect'):

QPushButton* ok = addTaskDialog.buttonBox->button(QDialogButtonBox::Ok);
connect(ok, &QPushButton::clicked, this, dialog.close());

Any help would be much appreciated!任何帮助将非常感激!

connect(ok, &QPushButton::clicked, this, dialog.close()); - your connect is wrong, you don't pass a pointer to a member function as forth parameter but the return value of dialog.close(). - 您的连接错误,您没有将指向成员函数的指针作为第四参数传递,而是将 dialog.close() 的返回值传递。 Also the context is wrong -> connect(ok, &QPushButton::clicked, &dialog, QQDialog::close) .上下文也错误 -> connect(ok, &QPushButton::clicked, &dialog, QQDialog::close)

btw: Your whole approach on how to create this dialog looks fishy - better create a new class derived from QDialog and do the stuff in there.顺便说一句:你关于如何创建这个对话框的整个方法看起来很可疑——最好创建一个从 QDialog 派生的新类并在那里做这些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM