QFileDialog bug

It seems QFileDialog::ShowDirsOnly does not work.  It is intended to show directories only. But it actually shows both files and directories.

The Qt official document talks about two options related to this problem. One is the file mode: “QFileDialog::Directory. The name of a directory. Both files and directories are displayed.” However it says if you want to show directories only, you should not only set the file mode to QFileDialog::Directory using setFileMode() function, but also set the option to QFileDialog::ShowDirsOnly using setOption(). I follow the instructions but the file dialog shows both directories and files and when you click a folder, it brings you to the content of the folder,i.e., you can not select the folder itself, but just a file in the folder. The official document also talks about another file mode:QFileDialog::DirectoryOnly and says it is obsolete, but I tried that and it works! Just using a single line of code  setFileMode(QFileDialog::DirectoryOnly), the file dialog will show just the directory, and you can select a folder.

Another problem about QFileDialog is how to select multiple files/directories. By default, you can only select one file or directory and you cannot press the Ctrl/Shift key to select multiple files.  By setting the file mode to QFileDialog::ExistingFiles, you can select multiple files in a folder. The function selectedFiles() will return all the selected file names. But in this mode, you still cannot select a folder, not even mention multiple folders. With the file mode set to QFileDialog::DirectoryOnly, you can select one folder. Unfortunately, you cannot combine QFileDialog::DirectoryOnly and QFileDialog::ExistingFiles together in order to select multiple folders. The two modes are exclusive. So how to select multiple directories  or select files and directories in the same time? I have not found a perfect solution to this problem. The most close one is in this qt thread where some internal functions are called to enable multiple selection. However, this method is not perfect, for example, you cannot select files only, you must select a folder first then select other files/folders.  Other technique to implement this function include subclassing the QFileDialog but that seems too complex. I hope Qt dev team can fix this problem and provide a simple way to enable multiple selection of files/folders.


Posted in

Leave a Reply