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. QFileDialog delete multiple files at once
Forum Updated to NodeBB v4.3 + New Features

QFileDialog delete multiple files at once

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 2.3k 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
    mcarter
    wrote on last edited by
    #1

    I have an app where I am using QFileDialog in 2 different ways.

    One way is for saving a file and once I set all the appropriate settings for QFileDialog, ot seems to work just fine. It even allows deleting a single file from the QFileDialog since for saving I allow only a single selection.

    The other way of course is for loading in a file or files using QFileDialog. I set things up to allow selecting multiple ExistingFiles. This all seems to work fine as well, except when it comes to deleting multiple files from the dialog. The dialog wants to ask the user whether to delete each file individually and if the user selects 'No' at any time, then the delete process stops. If a user highlights 20 files, then the process becomes ridiculous. I want a single request to delete all the selected files but I do not see that as an option. I also do not see a way to override the delete action.

    Is there a simple solution to attempt deleting all the files at once? I can understand if the file is write protected to ask for permission to delete, but not every file if simple permissions exist.

    Taz742T 1 Reply Last reply
    0
    • M mcarter

      I have an app where I am using QFileDialog in 2 different ways.

      One way is for saving a file and once I set all the appropriate settings for QFileDialog, ot seems to work just fine. It even allows deleting a single file from the QFileDialog since for saving I allow only a single selection.

      The other way of course is for loading in a file or files using QFileDialog. I set things up to allow selecting multiple ExistingFiles. This all seems to work fine as well, except when it comes to deleting multiple files from the dialog. The dialog wants to ask the user whether to delete each file individually and if the user selects 'No' at any time, then the delete process stops. If a user highlights 20 files, then the process becomes ridiculous. I want a single request to delete all the selected files but I do not see that as an option. I also do not see a way to override the delete action.

      Is there a simple solution to attempt deleting all the files at once? I can understand if the file is write protected to ask for permission to delete, but not every file if simple permissions exist.

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by Taz742
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        Although the answer is probably "no" in any case, are you using the native dialog or the Qt one, as they doubtless work differently? And if it's the native one, the behaviour doubtless depends on the platform too.

        M 1 Reply Last reply
        0
        • JonBJ JonB

          Although the answer is probably "no" in any case, are you using the native dialog or the Qt one, as they doubtless work differently? And if it's the native one, the behaviour doubtless depends on the platform too.

          M Offline
          M Offline
          mcarter
          wrote on last edited by
          #4

          @JonB I am using the Qt one. Does that mean I have to re-implement FileDialog myself to get the implementation that I need?

          JonBJ 1 Reply Last reply
          0
          • M mcarter

            @JonB I am using the Qt one. Does that mean I have to re-implement FileDialog myself to get the implementation that I need?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @mcarter
            TBH I don't know what it offers. If it does not offer what you want, then you have some reimplementation to do. You can derive from QFileDialog if needed. See also http://www.qtcentre.org/threads/69138-Adding-my-own-file-deletion-method-to-QFileDialog-getSaveFileName (someone else wanting to do something vaguely similar).

            M 1 Reply Last reply
            0
            • JonBJ JonB

              @mcarter
              TBH I don't know what it offers. If it does not offer what you want, then you have some reimplementation to do. You can derive from QFileDialog if needed. See also http://www.qtcentre.org/threads/69138-Adding-my-own-file-deletion-method-to-QFileDialog-getSaveFileName (someone else wanting to do something vaguely similar).

              M Offline
              M Offline
              mcarter
              wrote on last edited by
              #6

              @JonB lol, I saw that same thread, but noone has answered

              ugh, it seems reimplementation is a standard protocol when working with Qt, but hiding everything in a private class makes it so difficult.

              thanks for the info

              mrjjM 1 Reply Last reply
              0
              • M mcarter

                @JonB lol, I saw that same thread, but noone has answered

                ugh, it seems reimplementation is a standard protocol when working with Qt, but hiding everything in a private class makes it so difficult.

                thanks for the info

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

                @mcarter said in QFileDialog delete multiple files at once:

                but hiding everything in a private class makes it so difficult.

                Yeah, its the price of binary compatibility. sadly.

                Why not just use QFileDialog ability to select multiple files
                and simply loop over selection list and manually delete the files?
                That way you can ignore any that didn't get deleted and process rest of list ?

                M 1 Reply Last reply
                4
                • mrjjM mrjj

                  @mcarter said in QFileDialog delete multiple files at once:

                  but hiding everything in a private class makes it so difficult.

                  Yeah, its the price of binary compatibility. sadly.

                  Why not just use QFileDialog ability to select multiple files
                  and simply loop over selection list and manually delete the files?
                  That way you can ignore any that didn't get deleted and process rest of list ?

                  M Offline
                  M Offline
                  mcarter
                  wrote on last edited by
                  #8

                  @mrjj said in QFileDialog delete multiple files at once:

                  @mcarter said in QFileDialog delete multiple files at once:

                  but hiding everything in a private class makes it so difficult.

                  Yeah, its the price of binary compatibility. sadly.

                  not really, there could be methods to allow access to the inner workings and a lot more virtual methods, like 'virtual void deleteCurrent();' to get access to the delete process.

                  Why not just use QFileDialog ability to select multiple files
                  and simply loop over selection list and manually delete the files?
                  That way you can ignore any that didn't get deleted and process rest of list ?

                  but once the user chooses 'Delete' from the right-click menu, I do not have access to anything, becomes all internal. So I do not see how deriving from QFileDialog will help with anything unless I can replace the Delete action slot with my own implementation. Would that be possible?

                  jsulmJ 1 Reply Last reply
                  0
                  • M mcarter

                    @mrjj said in QFileDialog delete multiple files at once:

                    @mcarter said in QFileDialog delete multiple files at once:

                    but hiding everything in a private class makes it so difficult.

                    Yeah, its the price of binary compatibility. sadly.

                    not really, there could be methods to allow access to the inner workings and a lot more virtual methods, like 'virtual void deleteCurrent();' to get access to the delete process.

                    Why not just use QFileDialog ability to select multiple files
                    and simply loop over selection list and manually delete the files?
                    That way you can ignore any that didn't get deleted and process rest of list ?

                    but once the user chooses 'Delete' from the right-click menu, I do not have access to anything, becomes all internal. So I do not see how deriving from QFileDialog will help with anything unless I can replace the Delete action slot with my own implementation. Would that be possible?

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @mcarter Why would a user want to delete files inside QFileDialog? To me this sounds like a wrong use case. For deleting you usually provide a button/menu/... which opens a dialog where user selects files to delete and clicks "OK".

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

                    JonBJ 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @mcarter Why would a user want to delete files inside QFileDialog? To me this sounds like a wrong use case. For deleting you usually provide a button/menu/... which opens a dialog where user selects files to delete and clicks "OK".

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @jsulm
                      While I do not disagree with your sentiment, to be fair (so far as I know, for both Windows & Linux [Ubuntu Unity Desktop]), the native file dialog does allow user to delete files as well as load/save. For example, many games which allow user to load from a previous save also allow user to delete those saves from same dialog while looking at what saves exist. Though I have never tried to delete multiple files from there.

                      @mcarter

                      I set things up to allow selecting multiple ExistingFiles.

                      The way you write this implies to me that you are (perhaps) only allowing multiple-file-selection-for-load in order to allow multiple delete. If that is the case, you should not be allowing multiple multiple loads just to allow multiple deletes --- your load dialog should offer single/multiple only according as your program allows single/multiple loads.

                      M 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @jsulm
                        While I do not disagree with your sentiment, to be fair (so far as I know, for both Windows & Linux [Ubuntu Unity Desktop]), the native file dialog does allow user to delete files as well as load/save. For example, many games which allow user to load from a previous save also allow user to delete those saves from same dialog while looking at what saves exist. Though I have never tried to delete multiple files from there.

                        @mcarter

                        I set things up to allow selecting multiple ExistingFiles.

                        The way you write this implies to me that you are (perhaps) only allowing multiple-file-selection-for-load in order to allow multiple delete. If that is the case, you should not be allowing multiple multiple loads just to allow multiple deletes --- your load dialog should offer single/multiple only according as your program allows single/multiple loads.

                        M Offline
                        M Offline
                        mcarter
                        wrote on last edited by
                        #11

                        @jsulm
                        Having a QFileDialog just for deleting may be my final recourse, but the current Qt-based QFileDialog still has Delete as an option from the right-click menu and I do not think I can change that . . . so would still like that to work if it is an option for the user.

                        @JonB
                        The user wants the ability to load in multiple files so that is why the ExistingFiles on load; the multiple delete was just a nice byproduct, that I hoped worked out of the box. Is there really no way to get access to the delete process without a reimplementation of the widget?

                        DiracsbracketD 1 Reply Last reply
                        0
                        • M mcarter

                          @jsulm
                          Having a QFileDialog just for deleting may be my final recourse, but the current Qt-based QFileDialog still has Delete as an option from the right-click menu and I do not think I can change that . . . so would still like that to work if it is an option for the user.

                          @JonB
                          The user wants the ability to load in multiple files so that is why the ExistingFiles on load; the multiple delete was just a nice byproduct, that I hoped worked out of the box. Is there really no way to get access to the delete process without a reimplementation of the widget?

                          DiracsbracketD Offline
                          DiracsbracketD Offline
                          Diracsbracket
                          wrote on last edited by Diracsbracket
                          #12

                          @mcarter said in QFileDialog delete multiple files at once:

                          the current Qt-based QFileDialog still has Delete as an option from the right-click menu and I do not think I can change that

                          You can disable the 'delete' option though by setting the QFileDialog::ReadOnly option flag:

                              QFileDialog dialog(this);
                              dialog.setFileMode(QFileDialog::ExistingFiles);
                              dialog.setOptions(QFileDialog::ReadOnly | QFileDialog::DontUseNativeDialog);
                              dialog.exec();
                          

                          0_1525412043120_30e534e8-d7b5-4a80-be96-363587944bcc-image.png

                          So what remains to be done in order to provide the multiple-file selection Delete option is to provide a 3rd QFileDialog invokation to explicitly delete files then. This could be done by just using the open file dialog, and renaming the dialog's title and Accept button as follows:

                              QFileDialog dialog(this, tr("Delete File(s)"));
                              dialog.setFileMode(QFileDialog::ExistingFiles);
                              dialog.setOptions(QFileDialog::ReadOnly | QFileDialog::DontUseNativeDialog);
                          
                              dialog.setAcceptMode(QFileDialog::AcceptOpen);
                              dialog.setLabelText(QFileDialog::Accept, tr("Delete"));
                          
                              if (dialog.exec())
                              {
                                  qDebug() << dialog.selectedFiles();
                          
                                  //Delete all the files in the selection here
                                  ....
                              }
                          

                          0_1525414394577_2df0a6a4-de25-40ef-b589-fa67adfabab9-image.png

                          So now, Qt's implementation of the Delete operation is disabled since it can no longer be accessed via the context menu (although maybe the keyboard shortcuts could still work?) and you can implement your own code to delete the files without the nagging confirmation dialogs.

                          1 Reply Last reply
                          1

                          • Login

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