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. Is QFile::exists(const QString &file) misleading ?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.9k 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.
  • J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by J.Hilk
    #1

    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.


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    jsulmJ 1 Reply Last reply
    3
    • J.HilkJ J.Hilk

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

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

      kshegunovK 1 Reply Last reply
      4
      • jsulmJ jsulm

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

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        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.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          4

          • Login

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