Search code examples
c++qtqtwidgets

How to create dialog without blocking main form?


For now I can do:

void MainWindow::on_actionPATH_triggered() {
    std::unique_ptr<QDialog> win(new QDialog());
    win->exec();
}

Should I use async / run in separate threadto avoid blocking main window or is there way to subscribe to close even and delete / free object there?


Solution

  • You can use just show()

    void MainWindow::on_actionPATH_triggered() {
        QDialog* win = new QDialog();
        //needed connect
        win->setAttribute(Qt::WA_DeleteOnClose);//we don't want memory leak
        win->show();
    }
    

    and use

    win->setModal(false);//but it is default option, you don't need to change it
    

    From doc:

    By default, this property is false and show() pops up the dialog as modeless. Setting his property to true is equivalent to setting QWidget::windowModality to Qt::ApplicationModal. exec() ignores the value of this property and always pops up the dialog as modal.

    Qt::WA_DeleteOnClose will delete your dialog, when user close it.

    You can also set parent to dialog:

    QDialog* win = new QDialog(this);
    

    In this case win will be delete with your mainWindow.

    Info about Qt parent child relationship

    And you don't need here separate thread.