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. Ask for confirmation in QFileDialog

Ask for confirmation in QFileDialog

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.0k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    meikelneit
    wrote on last edited by
    #1

    Hello,

    i am using a QFileDialog to load files and create a TreeView out of the files data. But if a new one should be loaded i want the dialog to alert the user that the current TreeView will be deleted, so he has to confirm the loading. As i see in the docs, the QFileDialog::DontConfirmOverwrite is set to default and the cornfirmation is requested. But i dont get access to the dialog and does not show up. Anyone has a hint how i could do that?

    I use the code below to call the QFileDialog:

    QString l_filePath = QFileDialog::getIpenFileName(this,tr("Load Directory"),QDir::homePath(),("*.db"));

    then i pass this filePath to a regular open function.

    jsulmJ 1 Reply Last reply
    0
    • M meikelneit

      Hello,

      i am using a QFileDialog to load files and create a TreeView out of the files data. But if a new one should be loaded i want the dialog to alert the user that the current TreeView will be deleted, so he has to confirm the loading. As i see in the docs, the QFileDialog::DontConfirmOverwrite is set to default and the cornfirmation is requested. But i dont get access to the dialog and does not show up. Anyone has a hint how i could do that?

      I use the code below to call the QFileDialog:

      QString l_filePath = QFileDialog::getIpenFileName(this,tr("Load Directory"),QDir::homePath(),("*.db"));

      then i pass this filePath to a regular open function.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @meikelneit You can pass Options to the dialog: https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName. It's the last parameter.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        @meikelneit said in Ask for confirmation in QFileDialog:

        QFileDialog::DontConfirmOverwrite

        That confirmation only makes sense when you are trying to save a file. It won't show when you open it.

        If you need a custom behaviour like that, use custom QFileDialog, not one of the convenience functions.

        (Z(:^

        1 Reply Last reply
        4
        • M Offline
          M Offline
          meikelneit
          wrote on last edited by
          #4

          Ok thank you.
          Do i understand it right when i say, ill have to use my own QDialog *l_dialog = new QDialog;
          and then use a slot, which i code myself and it reacts on the QDialog::Accepted() Signal?

          mrjjM sierdzioS 2 Replies Last reply
          0
          • M meikelneit

            Ok thank you.
            Do i understand it right when i say, ill have to use my own QDialog *l_dialog = new QDialog;
            and then use a slot, which i code myself and it reacts on the QDialog::Accepted() Signal?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @meikelneit
            Hi
            as already mentioned, the Dialog can warn about overwriting files but it wont when loading them, so you could just do

            QString l_filePath = QFileDialog::getIpenFileName(this,tr("Load Directory"),QDir::homePath(),("*.db"));
            if (!filepath.empty)
            QMessageBox::warning(qApp->activeWindow(), "Replace", "Data will be lost. Continue ? ", QMessageBox::Yes|QMessageBox::No);
            

            before actually removing old TreeView

            1 Reply Last reply
            5
            • M meikelneit

              Ok thank you.
              Do i understand it right when i say, ill have to use my own QDialog *l_dialog = new QDialog;
              and then use a slot, which i code myself and it reacts on the QDialog::Accepted() Signal?

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @meikelneit said in Ask for confirmation in QFileDialog:

              Ok thank you.
              Do i understand it right when i say, ill have to use my own QDialog *l_dialog = new QDialog;
              and then use a slot, which i code myself and it reacts on the QDialog::Accepted() Signal?

              Yes. But you can create a QFileDialog - it will be much easier than creating custom implementation in "bare" QDialog.

              (Z(:^

              1 Reply Last reply
              2
              • M Offline
                M Offline
                meikelneit
                wrote on last edited by meikelneit
                #7

                I have a Problem now and i dont get the Point i am missing.

                I tryed two Solutions:

                QMessageBox l_Warning;
                l_Warning.setText("Beim importieren der Datei gehen die aktuellen Daten verloren.");
                l_Warning.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
                l_Warning.setWindowTitle("Warnung");
                QPushButton *l_laden = l_Warning.addButton((tr("Laden"),QMessageBox::Yes));
                l_Warning.exec();

                if(l_Warning.buttonClicked() == l_laden) here appears an Error: Cant convert QPushButton* to QAbstractButton*
                {
                …
                }

                QMessageBox l_Warning;
                l_Warning.setText("Beim importieren der Datei gehen die aktuellen Daten verloren.");
                l_Warning.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
                l_Warning.setWindowTitle("Warnung");
                QAbstractButton *l_laden = l_Warning.addButton((tr("Laden"),QMessageBox::Yes)); here appears an Error: Cant convert QAbstractButton* to QPushButton*
                l_Warning.exec();

                if(l_Warning.buttonClicked() == l_laden)
                {
                …
                }

                What am is missing?

                PS: I guess its the Action role QMessageBox::Yes, but what else could i set there, i dont see flags in the doc for Abstract Buttons.

                jsulmJ 1 Reply Last reply
                0
                • M meikelneit

                  I have a Problem now and i dont get the Point i am missing.

                  I tryed two Solutions:

                  QMessageBox l_Warning;
                  l_Warning.setText("Beim importieren der Datei gehen die aktuellen Daten verloren.");
                  l_Warning.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
                  l_Warning.setWindowTitle("Warnung");
                  QPushButton *l_laden = l_Warning.addButton((tr("Laden"),QMessageBox::Yes));
                  l_Warning.exec();

                  if(l_Warning.buttonClicked() == l_laden) here appears an Error: Cant convert QPushButton* to QAbstractButton*
                  {
                  …
                  }

                  QMessageBox l_Warning;
                  l_Warning.setText("Beim importieren der Datei gehen die aktuellen Daten verloren.");
                  l_Warning.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
                  l_Warning.setWindowTitle("Warnung");
                  QAbstractButton *l_laden = l_Warning.addButton((tr("Laden"),QMessageBox::Yes)); here appears an Error: Cant convert QAbstractButton* to QPushButton*
                  l_Warning.exec();

                  if(l_Warning.buttonClicked() == l_laden)
                  {
                  …
                  }

                  What am is missing?

                  PS: I guess its the Action role QMessageBox::Yes, but what else could i set there, i dont see flags in the doc for Abstract Buttons.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @meikelneit Shouldn't it be clickedButton()?
                  If it then still complains then do the cast:

                  if(l_Warning.buttonClicked() == reinterpret_cast<QAbstractButton*>(l_laden))
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • M Offline
                    M Offline
                    meikelneit
                    wrote on last edited by
                    #9

                    Ok thank you, that works. But why do i needthe cast there, in the first step i just copied my Code from the docs, ist exactly like that.
                    https://doc.qt.io/qt-5/qmessagebox.html see QAbstractButton.

                    jsulmJ 1 Reply Last reply
                    0
                    • M meikelneit

                      Ok thank you, that works. But why do i needthe cast there, in the first step i just copied my Code from the docs, ist exactly like that.
                      https://doc.qt.io/qt-5/qmessagebox.html see QAbstractButton.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @meikelneit What compiler do you use?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        meikelneit
                        wrote on last edited by
                        #11

                        I use the Compiler MSVC15

                        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