Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] QFileDialog and cancel button
QtWS25 Last Chance

[solved] QFileDialog and cancel button

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 15.1k Views
  • 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 Offline
    R Offline
    Rizokuri
    wrote on last edited by
    #1

    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
    0
    • N Offline
      N Offline
      Nosf
      wrote on last edited by
      #2

      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
      0
      • M Offline
        M Offline
        maxim.prishchepa
        wrote on last edited by
        #3

        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
        0
        • R Offline
          R Offline
          Rizokuri
          wrote on last edited by
          #4

          Thank you it works!

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved