QFileDialog delete multiple files at once



  • 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.



  • This post is deleted!


  • 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.



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



  • @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).



  • @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


  • Qt Champions 2017

    @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 ?



  • @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?


  • Moderators

    @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".



  • @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.



  • @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?



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.