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. Universal solution for resource prefix.
Forum Updated to NodeBB v4.3 + New Features

Universal solution for resource prefix.

Scheduled Pinned Locked Moved General and Desktop
13 Posts 7 Posters 19.9k Views 1 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.
  • V Offline
    V Offline
    vsorokin
    wrote on last edited by
    #1

    You know what for calling items from resource used ":/" prefix.
    but its don't work for QUrl, its needed direct order of protocol "qrc:/".

    I want keep prefix in settings. It might be resource or local files, and I need in universal solution without additionaly checks.

    Any ideas?

    --
    Vasiliy

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      The resource system behaves a bit like the file system. If you use QUrl to access local files, the pathn may look different as well (on windows it most certainly is, haven't checked on linux). Maybe QUrl::toLocalFile() produces the ":/" format?

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vsorokin
        wrote on last edited by
        #3

        This code
        @ QUrl url("qrc:/images/image.gif");
        qDebug() << url.toLocalFile();
        @
        shows empty string :(

        I show example of what I want:

        @
        ...
        QString Settings::resourcePath()
        {
        return ":"; // or it may be "/home/user/program/data/" path
        }

        ...

        QListWidgetItem *item = new QListWidgetItem("Item" , QIcon(settings->resourcePath() + "/icons/item.png"));

        ...

        QDeclarativeView view;
        ...
        view.setSource(QUrl(settings->resourcePath() + "/scripts/main.qml")); // Don't work for ":", QUrl think that this is local path.

        @

        --
        Vasiliy

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          QUrl::toLocalFile() only works on URLs with "file:" or empty protocol. For all other protocols (like "qrc:") an empty string is returned.

          You should return "file:///home/user/path/to/resource/" or "qrc:/" as the resource prefixes.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          1
          • V Offline
            V Offline
            vsorokin
            wrote on last edited by
            #5

            Volker
            Settings::resourcePath() can return ':' or local path or (may be) http url, it is setting and user will set it in configuration file.
            And it worked for url and local file, but it don't worked for resources, because QUrl don't able convert ":/" prefix to qrc protocol

            --
            Vasiliy

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Please fix the post - the Quotes add a link, maybe you should wrap it to the source tags.

              For QUrl: you must add the qrc scheme (protocol) to the string!

              @
              // ok:
              QUrl validUrl("qrc:/path/to/main.qml");

              // bad:
              // bascially resolves to "file:///path/to/main.qml"
              QUrl badUrl(":/path/to/main.qml"); // bad
              @

              The latter is treated like a file:// URL.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vsorokin
                wrote on last edited by
                #7

                Volker Yes! I know it. And I think that it don't right, because I must add checking whenever I want use my prefix in QUrl. See my example.

                --
                Vasiliy

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  So why not add two methods to your Settings:

                  @
                  QString prefix; // set it to ":/" or "/path/to/home"

                  QString getFilePrefix() {
                  return prefix;
                  }

                  QString getUrlPrefix() {
                  return prefix.startsWith(":/") ? "qrc" + prefix : prefix;
                  }
                  @

                  And then use the right one - you know in advance when if you construct an URL or a file path.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vsorokin
                    wrote on last edited by
                    #9

                    It is not pretty.
                    But it seems the only way.

                    --
                    Vasiliy

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      bunjee
                      wrote on last edited by
                      #10

                      Interesting post.

                      From my perspective when you set a QUrl to "qrc:/myFile" then QUrl.toLocalFile() should return ":/myFile" instead of an empty string.

                      I guess there is a good reason for not doing this.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        [quote author="bunjee" date="1302365361"]Interesting post.

                        From my perspective when you set a QUrl to "qrc:/myFile" then QUrl.toLocalFile() should return ":/myFile" instead of an empty string.

                        I guess there is a good reason for not doing this.[/quote]

                        At least at first sight, I think that would be a sensible thing to do. Why don't you file a feature request for that on Jira. If it isn't possible, you might get a response explaining why that would be the case.

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          just a small update here.

                          I have tried adding an image file from outside of qrc file and it works successfully. I have used something like this,

                          @QString path = "D:/logo.png";
                          engine.rootContext()->setContextProperty("logoPath", QUrl::fromLocalFile(path));
                          @
                          The "logoPath" can be used in the QML file, for example
                          @
                          Image
                          {
                          source: logoPath
                          }@

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            pls13
                            wrote on last edited by
                            #13

                            The trick to make QDir (QFile/QPixmap and others) to accept and work with QUrl pointing to both local file ("file:" schema or no schema) or QRC ("qrc:" schema) is to add a search path:

                            QDir::setSearchPaths("qrc", QStringList(":/"));
                            

                            Now for the following

                            QUrl url("qrc:///images/image.gif");
                            QDir(url.toString()).exists();         
                            

                            will work as File System will consider "qrc" as search path and finally will look for ":///images/image.gif".
                            NOTE: the use of toString(), not toLocalFile().

                            Obviously using the same url for anything that needs QUrl will work too.

                            QDeclarativeView view;
                            view.setSource(url);
                            
                            1 Reply Last reply
                            3

                            • Login

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