Home > database >  How to close a QDialog that uses a different ui file in Qt?
How to close a QDialog that uses a different ui file in Qt?

Time:07-17

I've been trying to close a QDialog that uses a separate ui file. The dialog is used in a slot of a different class (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. 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).

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:

  • ok -> closes the dialog
  • cancel -> closes and deletes the dialog.

I have tried something like this, but it doesn't work (I get this build issue: "No matching member function for call to 'connect'):

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

Any help would be much appreciated!

CodePudding user response:

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(). Also the context is wrong -> 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.

  • Related