Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to open an asynchronous application modal file dialog



  • It seems like I can't do this without compromise. I'm trying to open the dialog like this:

    auto *dialog = new QFileDialog;
    dialog->setAttribute(Qt::WA_DeleteOnClose);
    dialog->setAcceptMode(QFileDialog::AcceptOpen);
    dialog->setNameFilter("*.png");
    dialog->setFileMode(QFileDialog::ExistingFile);
    dialog->setDirectory("directory");
    CONNECT(dialog, fileSelected, this, openFile);
    dialog->open();
    

    The dialog doesn't appear. Am I doing something wrong?

    If I use show() then this works but then I have another problem. Setting the directory before calling show() doesn't set the directory! So I have to set the directory after showing the dialog but then the user sees the dialog changing directory. There's a flicker where the dialog is changing directory. So here's the code that sort of works:

    // ...
    dialog->show();
    dialog->setDirectory("directory");
    

    I can also use exec() instead of show() that the lets me set the directory before showing the dialog like this:

    // ...
    dialog->setDirectory("directory");
    dialog->exec();
    

    I was under the impression that synchronous dialogs were bad and that I should use asynchronous dialogs instead. That's what the docs say. What could cause open() to just not do anything?



  • @Kerndog73 said in How to open an asynchronous application modal file dialog:

    The dialog doesn't appear

    Are you sure? open() does nothing but setting the modality of the window and then calls show() so it's strange that it doesn't work

    I was under the impression that synchronous dialogs were bad and that I should use asynchronous dialogs instead.

    Kinda... the mentioned "bug" is illustrated in this article. Strictly speaking it's better to use async even for modal dialogs but the downsides are performance and some very fringe case crash so I wouldn't be too worried about using exec() if it achieves what you want


Log in to reply