Nominate our 2022 Qt Champions!

How to make a filedialog modal in a console application

  • Hello forum,

    I wrote an application that has no GUI, just two filedialogs.
    The application performs three simple steps:

    • Reading data from a file
    • Manipulate data using some formula
    • Store the result into another (new) file

    To determine the two filenames the filedialogs are used. The little program works fine, but there is one spot of bother: The filedialogs can be set into the background. This is unconvienient for the user, because he has to search for the dialogs. To solve this problem I want to set the filedialogs to "modal", but I had no success with this.
    I tested several variants but without any reuslt. Here is a fragment of my code. In the comments are some variants that I tested.

    QFileDialog inputFileDlg(Q_NULLPTR, "File to open", "MyStartPath", "MyFiles (*.xyz)");

    //inputFileDlg.setWindowFlag(Qt::WindowStaysOnTopHint, true);
    //QString defaultFileName = inputFileDlg.getOpenFileName(Q_NULLPTR, "File to open", "MyStartPath", "MyFiles (*.xyz)");
    // Finish if the file dialog is terminated

    // if (defaultFileName == "")
    // {
    // return;
    // }

    if (inputFileDlg.exec() != QDialog::Accepted)

    //Proceed with data manipulation

    Does anyone have an idea?
    Thanks in advance.

    Best regards

  • Lifetime Qt Champion

    I think it needs a widget based parent to stay on top (of parent)
    A shell window wont do im afraid.
    But i could be wrong and someone knows a trick :)

Log in to reply