Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. QWebView won't load file from qrc resource
Forum Updated to NodeBB v4.3 + New Features

QWebView won't load file from qrc resource

Scheduled Pinned Locked Moved Solved Qt WebKit
16 Posts 6 Posters 15.7k 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.
  • E Offline
    E Offline
    elkvis
    wrote on last edited by A Former User
    #1

    I have a .qrc file in my project, which is basically the default html5 template project, and my web view cannot load my index.html file via qrc. the qrc resources are accessible when I try to open them with QFile, but if I use the exact same qrc url with my web view, I get nothing displayed. I've observed this behavior with Qt 5.2.1 and with 5.3.0. I've used the "Copy Resource Path to Clipboard" option in the qrc editor, and the resulting path works with QFile, but not with the web view. I'd expect that QWebView and QGraphicsWebView use QFile on the back end, but for some reason, it just won't show my html page.

    is there something special about QWebView or QGraphicsWebView that prevents qrc resources from working? is there another step I'm missing?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Just an educated guess but did you try with

      @"qrc:/path/to/index.html"@

      ?

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        elkvis
        wrote on last edited by
        #3

        unfortunately, I tried with every combination of:

        qrc:/html/index.html
        qrc://html/index.html
        qrc:///html/index.html
        :/html/index.html
        ://html/index.html

        maybe a few others. I've tried so many things, I've lost track of all of them.
        @QFile file(":/html/index.html");@
        works just fine, but
        @viewer->webView()->load(QUrl(":/html/index.html"));@
        does not.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          Have you tried ?
          @
          viewer->webView()->load(QUrl::fromLocalFile(":/html/index.html"));
          @

          EDIT: Did you check what QUrl(":/html/index.html") returns

          1 Reply Last reply
          0
          • E Offline
            E Offline
            elkvis
            wrote on last edited by
            #5

            I'll have to pick this up again on tuesday, as my work week is over.

            1 Reply Last reply
            0
            • E Offline
              E Offline
              elkvis
              wrote on last edited by
              #6

              right-clicking on index.html in the qrc editor, and selecting "Copy Resource Path to Clipboard" gives "://index.html"

              this code
              @
              QDir dir(":/");
              for (QFileInfo& e : dir.entryInfoList())
              {
              std::cout << "File: " << e.absoluteFilePath() << std::endl;
              }
              @

              produces the following output:

              File: :/index.html
              File: :/items
              File: :/qt-project.org
              File: :/scenegraph
              File: :/webkit

              I have tried both versions of the path, and neither works with the web view, even with QUrl::fromLocalFile(), but both work with QFile.

              QUrl(":/index.html") shows "://://index.html" and QUrl::fromLocalFile(":/index.html") shows "file://:/index.html" in the debugger locals window.

              Changing the url to "qrc:/index.html" causes both the web view and QFile to fail to open the file.

              Can one of you try this to see if you get the same results?

              steps to reproduce:

              1. create a new html5 project from the default qt creator template.
              2. add a qrc file to your project.
              3. add your index.html file to the qrc file.
              4. use the qrc path instead of the local file path when loading the web view.
              5. run and watch it fail to load the html file.
              1 Reply Last reply
              0
              • A Offline
                A Offline
                andreyc
                wrote on last edited by
                #7

                I looked at QUrl source code.
                It does not support qrc: prefix.
                A solution could be to copy index.html from the resource to a temporary file and then use QUrl::fromLocalFile("file://temp_path/index.html");

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  elkvis
                  wrote on last edited by
                  #8

                  I wouldn't really consider that a solution. The web view has to load additional resources (css, js, images), based on the content of the html, and I'd have to literally copy everything to the temp folder. Once this project starts to get big, that's simply not an option.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andreyc
                    wrote on last edited by
                    #9

                    I agree with you. It is not convenient. But I don't see the other way.

                    I see two issues with putting all html related files as the resources:

                    When you put all html, js, etc files in the executable file as the resources, how are you going to manage the links inside these html, js, etc files?

                    If you will put a lot of resources in the executable file it will make the file big and relatively slow to load.

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      elkvis
                      wrote on last edited by
                      #10

                      can you suggest alternatives, so that the content of my app isn't just sitting there on the user's computer to manipulate as they please?

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

                        I think Qt assistant maybe a good example. Take a look on how they provide and show bunch of html files.

                        Or something simple like this

                        • Extract all files to OS dependent cache directory on first run or after installation
                          @
                          QString cacheDir =
                          QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
                          @

                        • Load the files from the cache directory

                        • Provide checksum for the files if you need to protect an app from loading the modified files.

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          elkvis
                          wrote on last edited by
                          #12

                          Thanks for the suggestion. I used exactly what you suggested (except TempLocation instead of CacheLocation), and recursively copied the contents of the resources to the destination folder. Everything works, and files are deleted automatically on program exit on my target platform (linux).

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andreyc
                            wrote on last edited by
                            #13

                            Glad it works for you.
                            Could you please prepend "[SOLVED]" to the title of your post. It will help other good people to find your solution.

                            1 Reply Last reply
                            0
                            • O Offline
                              O Offline
                              olgii10
                              wrote on last edited by
                              #14

                              If you're reading from a .qrc resource file you have to run qmake ("Build->Run qmake" in Qt Creator) before it will be available.

                              1 Reply Last reply
                              0
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #15
                                This post is deleted!
                                1 Reply Last reply
                                1
                                • D Offline
                                  D Offline
                                  Donald Duck
                                  wrote on last edited by
                                  #16

                                  For me, using the qrc:/ protocol (qrc:/html/index.html) along with cleaning the project and running qmake worked. But it didn't work by simply using :/html/index.html or before cleaning the project and running qmake.

                                  1 Reply Last reply
                                  0

                                  • Login

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