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. How to check if a resource exists?
Forum Updated to NodeBB v4.3 + New Features

How to check if a resource exists?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 4.4k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I'm trying to defeat the QML messages that come up when an image isn't present or doesn't exist.

    In my C++ function I have written a helper function:

    QString Manager::checkPath(QString path) {
        bool valid = false;
        if ( path.length() > 0 && path.indexOf(".") > 0 ) {
            const String QRCprefix("qrc:");
            if ( path.startsWith(QRCprefix) != true ) {
                const QString imgsPath("/images/");
                if ( path.startsWith(imgsPath) != true ) {
                  path += imgsPath;
                }
                path = QRCprefix + path;
            }
            valid = QFile::exists(path);
        }
        return (valid == true) ? path : "";
    }
    

    I'm calling this from the QML, as an example I can see in Qt Creator a valid reference is passed:

    qrc:/images/ImageViewer/viewer_camera_rear2_off.png
    

    However, when it gets to call QFile::exists(), this returns false. I've read elsewhere on these forums that QFile::exists can be used on resources, however my findings suggest either I'm doing something wrong or this isn't the case.

    Can anyone tell me?

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #4

      Last resort is to check ":/images" but I suspect it to fail as well.

      Can you paste here the relevant line from your .qrc file?

      (Z(:^

      1 Reply Last reply
      3
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        @SPlatten said in How to check if a resource exists?:

        qrc:

        QFile does not understand this part. In order for QFile to get it, you need a different URL:

        qrc:///images/ImageViewer/viewer_camera_rear2_off.png
        

        (Z(:^

        1 Reply Last reply
        1
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by SPlatten
          #3

          No difference, still returns false.

          Have tried:

          qrc:/images/ImageViewer/viewer_camera_rear2_off.png
          qrc://images/ImageViewer/viewer_camera_rear2_off.png
          qrc:///images/ImageViewer/viewer_camera_rear2_off.png

          All return false, yet the path is valid.

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #4

            Last resort is to check ":/images" but I suspect it to fail as well.

            Can you paste here the relevant line from your .qrc file?

            (Z(:^

            1 Reply Last reply
            3
            • SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              I am trying that now, funny enough I thought the same, refreshed and saw your suggestion.

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #6
                :/images/ImageViewer/viewer_camera_rear2_off.png
                

                Works! And now QFile::exists returns true, thank you!

                Kind Regards,
                Sy

                1 Reply Last reply
                2
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #7

                  Nice! Happy coding

                  (Z(:^

                  1 Reply Last reply
                  1

                  • Login

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