Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFileDialog::selectedFiles()



  • The help says: If no files are selected, or the mode is not ExistingFiles or ExistingFile, selectedFiles() contains the current path in the viewport.

    So does that mean that I need to check that the returned string (if using ExistingFile) is a regular file rather then a directory?

    If so, what is the preferred way to do that?

    Thanks, David



  • @Perdrix
    Start with: if you're not using "Existing..." then you don't even know that the file/path exists, so it won't be necessarily be either.

    What are you using the selected file for?



  • I am using ExistingFile mode - this is to read a config file ... so there may not be one there to read, or the user may not select one even it there are several to choose from.

    Similar considerations I guess when choosing a file to save to.

    David



  • @Perdrix said in QFileDialog::selectedFiles():

    so there may not be one there to read, or the user may not select one even it there are several to choose from.

    So then why are you asking if no selection will be a directory?! :)

    If you specify "existing file", you'll only get an existing file. Yes, you have to check for Cancel/no selection.



  • I'm asking because the help says that no selection will return the directory.

    So I need to know how to check that.

    D.



  • @Perdrix
    OK, I see that. So use QDir or QFileInfo on the returned path to check what it is.

    Still not sure you'll be looking at selected files, I think the dialog doesn't let user click OK if nothing/wrong thing selected.

    Also, not sure why you need this method. It depends what you're doing, but most people use one of the static methods listed in QFileDialog to get just what they want, and they return what you want without needing selectedFiles(). For loading one file there is QString QFileDialog::getOpenFileName() and for saving one file there is QString QFileDialog::getSaveFileName().



  • Having looked at the source code, it would appear that getOpenFileName() and getSaveFileName () just return what is effectively selectedFiles()[0] so AFAICT, the QString they return should also be checked using QFileInfo::isFile()

    David



  • @Perdrix
    https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName

    returns an existing file selected by the user. If the user presses Cancel, it returns a null string.

    Show me how you manage to select no file, or the directory you're looking in, and get the directory back?

    I certainly suggest in your case you use QFileInfo, so that you can check whatever attributes you wish to. Not to mention, another process could have changed anything about the file between the time you select it and the time you use it in your code. The only way you truly know what the situation is with a file path is when you open it for read/write, at which point you'll get an EISDIR error (probably) and have to deal with that,

    I'm not going to debate this any more. I have used these functions for ages without a directory being selected. I don't know why you didn't try it yourself by now, and if you're good enough to look in the code for yourself you don't need me/anyone else to spend time answering your questions as you have your own answer.


Log in to reply