Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How do I determine whether a file exists?

How do I determine whether a file exists?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 4 Posters 9.6k Views
  • 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.
  • D Offline
    D Offline
    drmhkelley
    wrote on 13 May 2018, 21:04 last edited by
    #1

    Specifically, I want to specify the source file for a QML Image by something like

    myImage.source = '../resources/' + somename + '.png'

    This works just fine as long as the specified image file exists.

    How can I validate that it exists and take some appropriate action if it does not?

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Eligijus
      wrote on 14 May 2018, 05:27 last edited by
      #2

      test for undefined

      myImage.source: {
      var path = '../resources/' + somename + '.png'
      if (path !== undefined)
        return path
      else
        return ""
      }
      

      and inline version:

      myImage.source:  '../resources/' + somename + '.png' ?  '../resources/' + somename + '.png' : ""
      
      1 Reply Last reply
      2
      • D Offline
        D Offline
        drmhkelley
        wrote on 14 May 2018, 13:31 last edited by
        #3

        @Eligijus said in How do I determine whether a file exists?:

        test for undefined

        Thanks. Unfortunately, I've tried that and it doesn't work.

        In the example, path is defined, but because the statement "path = " defines it.

        What I need to determine is whether the file (as defined by path) exists or not.

        But maybe I've missed or mis-understood something?

        E 1 Reply Last reply 14 May 2018, 14:12
        2
        • D drmhkelley
          14 May 2018, 13:31

          @Eligijus said in How do I determine whether a file exists?:

          test for undefined

          Thanks. Unfortunately, I've tried that and it doesn't work.

          In the example, path is defined, but because the statement "path = " defines it.

          What I need to determine is whether the file (as defined by path) exists or not.

          But maybe I've missed or mis-understood something?

          E Offline
          E Offline
          Eligijus
          wrote on 14 May 2018, 14:12 last edited by Eligijus
          #4

          @drmhkelley You are correct, I confused it with something different. And from the looks of it you can't do that check from qml side. You can check from c++ side with QFile::exists() method http://doc.qt.io/qt-5/qfile.html#exists
          Also if you are playing with relative urls, Qt.resolvedUrl() could come in handy for you http://doc.qt.io/qt-5/qml-qtqml-qt.html#resolvedUrl-method .

          1 Reply Last reply
          0
          • D Offline
            D Offline
            drmhkelley
            wrote on 14 May 2018, 20:56 last edited by
            #5

            Thanks. Qt.resolvedUrl() was one of the things I had tried, with same results as your initial suggestion.

            I feared that QML wouldn't suffice and that I'd need to retreat to Qt C++ directly. Sadly, this isn't the first time I've bumped into that.

            I've now made a couple of forays into QML because it seems so simple and because some relevant Qt provided examples were built in QML. However, thus far EVERY project that I've started in QML ultimately required a full port into C++ because necessary functionality just wasn't there.

            I'll cut my losses and write this one off a just another of my failures to learn from prior mistakes :(

            1 Reply Last reply
            1
            • G Offline
              G Offline
              GrecKo
              Qt Champions 2018
              wrote on 15 May 2018, 11:13 last edited by
              #6

              You can expose c++ helpers to extend QML functionnality.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                drmhkelley
                wrote on 15 May 2018, 21:34 last edited by
                #7

                Yes, that is possible.

                But as far as I know, such an approach to my present issue would require messing was the source for the underlying Qt libs themselves - for the current and all future releases. Not sure that’s a path I want to follow too far.

                Am I missing something?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  GrecKo
                  Qt Champions 2018
                  wrote on 15 May 2018, 22:40 last edited by
                  #8

                  You don't need to mess with Qt Sources to do that.
                  Create a new class subclassing QObject, add and implement a bool fileExists(const QString& fileName) Q_INVOKABLE/slot. Register the class with qmlRegisterSingletonType().

                  After that you can do in QML import YourModuleName 1.0 and then YourClass.fileExists(fileName).

                  1 Reply Last reply
                  3
                  • E Offline
                    E Offline
                    ekkescorner
                    Qt Champions 2016
                    wrote on 17 May 2018, 13:58 last edited by
                    #9

                    @GrecKo already told you how easy it is to check if file exists from QML invoking a C++ method.
                    be aware if your file is located anywhere at the file system, there's a small difference between QML and C++ file pathes:
                    QML file pathes are prefixed by file://
                    checking those QML pathes from C++ you must remove file:// before checking if exists

                    ekke ... Qt Champion 2016 | 2024 ... mobile business apps
                    5.15 --> 6.8 https://t1p.de/ekkeChecklist
                    QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

                    1 Reply Last reply
                    2
                    • D Offline
                      D Offline
                      drmhkelley
                      wrote on 20 May 2018, 23:03 last edited by
                      #10

                      Thanks. I seem to be acquiring quite a library of such QML extensions to cover what seem to be significant deficiencies of an otherwise pretty impressive programming platform. However, it's still not completely clear that QML has been a net gain in capability. So far, I'm writing that off to my own inexperience, but it remains an issue of active contemplation.

                      1 Reply Last reply
                      1

                      2/10

                      14 May 2018, 05:27

                      topic:navigator.unread, 8
                      • Login

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