⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

[solved] QFileDialog and cancel button

  • Hi,

    How do I detect when the cancel button has been pressed by the user? Using the static QFileDialog, the file selected should be null but when instantiating a QFileDialog "manually", the file selected is not null...

    QFileDialog dlg( NULL, tr("Save file"));
    dlg.setAcceptMode( QFileDialog::AcceptSave );

    QString fileName = dlg.selectedFiles().at(0);
    // fileName is not null when user pressed cancel!


  • Try

    QFileDialog dlg(NULL, tr("Save file"));
    QString file_name;
    if (dlg.exec())
    file_name = dlg.selectedFiles().at(0);
    // User Hit Cancel

    If your not using the model dialog you can use finished() and result() to check the dialog's reply as well in a similar way.

  • for what you need this? if you try to understand, what dialog state was after closing, then use:

    @QFileDialog dlg( NULL, tr("Save file"));
    QString fileName;
    if(dlg == QDialog::Accepted){
    fileName = dlg.selectedFiles().at(0);

  • Thank you it works!

    I need this because I'm adding a widget at the end of my QFileDialog layout...

Log in to reply