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

Is QFile::exists(const QString &file) misleading ?


  • Moderators

    Hi,
    Let me ask this question.

    If you have a QString representing a system path - for example - "C:/temp/" and the folder exists.

    What would you expect to return from the static call to QFile::exists ?

    bool exists = QFile::exists("C:/temp/");
    

    From my understanding exists would be false, as that is not a file, but a folder.

    But, looking into QFile::exists code, there's no check for QFileInfo::isFile() or QFileInfo::isDir(), just a static call to "QFileInfo::exists(fileName)"

    Therefore the returned value is in fact true.

    • Was that always the case ?
    • Is that something I should make the effort to try to get changed
    • should I just make a QFileInfo object and check for exists and isFile manually ? (probably ;-) )
    • Am I just chasing windmills at this point and it's time for the weekend

    Greetings.


  • Lifetime Qt Champion

    @J.Hilk Isn't everything a file on UNIX? :-)
    But I agree more intoitive would be: QFile::exists() checks whether is is a file and QDir::exists() checks whether it is a directory.


  • Qt Champions 2017

    I'd've expected exactly what you observed.

    @jsulm said in Is QFile::exists(const QString &file) misleading ?:

    @J.Hilk Isn't everything a file on UNIX? :-)

    It is, but that's beside the point. Directories are files on windows as well.



  • There's currently in progress (with the support of Thiago) a redesign of QFile to separate the QIODevice from the filesystems operations, it might be worth raising this point there: https://lists.qt-project.org/pipermail/development/2019-February/035155.html

    IMHO I'd just remove the method altogether and leave it in QFileInfo only as you can fine-tune it a lot more there


Log in to reply