Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [solved] QFileDialog and cancel button

    General and Desktop
    3
    4
    13977
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      Rizokuri last edited by

      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 );
      dlg.exec();

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

      Thanks

      1 Reply Last reply Reply Quote 0
      • N
        Nosf last edited by

        Try

        @
        QFileDialog dlg(NULL, tr("Save file"));
        dlg.setAcceptMode(QFileDialog::AcceptSave);
        QString file_name;
        if (dlg.exec())
        file_name = dlg.selectedFiles().at(0);
        else
        // 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.

        1 Reply Last reply Reply Quote 0
        • M
          maxim.prishchepa last edited by

          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);
          }@

          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

          1 Reply Last reply Reply Quote 0
          • R
            Rizokuri last edited by

            Thank you it works!

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post