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. Accessing qrc resources
Forum Updated to NodeBB v4.3 + New Features

Accessing qrc resources

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 2.0k 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.
  • N Offline
    N Offline
    Nicolas Leonard
    wrote on last edited by
    #1

    Hello, first time posting here.

    I am fairly new to the Qt world. I am working on creating a windows dll with Visual C++.

    My problem is that I cannot seem to load icons from qrc resources. If I load the images from external files, there is no problem. But no success loading the icons from the resources.

    ⦁ I have created a DrivenControler.qrc file to my vs vcxproj project
    ⦁ I added the image files to the resource file in the Qt Resource Editor and everything looks fine, resulting in resource URLs like ":/DrivenController/Images/muted.png"
    ⦁ The qrc file compiles:
    1>------ Rebuild All started: Project: DrivenController, Configuration: Debug x64 ------
    1>Uic'ing DrivenManagerDialog.ui...
    1>Rcc'ing DrivenController.qrc...
    [...]
    and generates a qrc_DrivenController.cpp file, which gets compiled in turn:
    1>qrc_DrivenController.cpp
    1> Creating library C:\Users[...]DrivenController.lib

    ⦁ Loading the icon from an external file QIcon s_mutedIcon("C:\Users\[...]Images\muted.png")
    works fine so there should be nothing wrong with the image file itself. Yet trying to load it from resource fails:
    QIcon mutedIcon(":/DrivenController/Images/muted.png");
    A pixmap constructed with the resources is Null.
    ⦁ I tried to enumerate all resources using this snippet:
    QDirIterator iter(":", QDirIterator::Subdirectories);
    while (iter.hasNext()) {
    qDebug() << iter.next();
    }
    ":/DrivenController/Images/muted.png" is missing from the list, as are all resources from DrivenControler.qrc.

    So it looks like the problem is that the content of DrivenControler.qrc does not make its way to available Qt resources. Does anyone have suggestions or a clue what I am missing?

    Thanks for suggestions

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

      Hi and welcome to devnet,

      Since you are talking about dll, I think you are in the case described here in the Qt Resources documentation.

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

      N 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi and welcome to devnet,

        Since you are talking about dll, I think you are in the case described here in the Qt Resources documentation.

        N Offline
        N Offline
        Nicolas Leonard
        wrote on last edited by
        #3

        @SGaist

        Thanks a lot!

        It looks like a call to Q_INIT_RESOURCE should have fixed the issue, but has not.

        I tried:
        ⦁ recreating the qrc file from scratch, and giving it a distinct name ('QtResource.qrc') in case there would be an unexpected name clash
        ⦁ calling Q_INIT_RESOURCE(QtResource) at misc points in time, from the DLL_PROCESS_ATTACH in DllMain, to in the ctor of a static library variable, to before use. No matter where I place it, the project qt resources are not among the resources listed enumerating all resources with the above code snippet, and unsurprisingly icon creation fails.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Please post exactly what you try to do (do you use a static lib?) and some example code incl. an example qrc file.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          N 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Please post exactly what you try to do (do you use a static lib?) and some example code incl. an example qrc file.

            N Offline
            N Offline
            Nicolas Leonard
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            Thank you for your offer to help!

            I am creating a dll not a static lib, and failing to access the Qt resources inside my dll code, not from the app code.
            Say I have two image files in file QtResouce,qrc. Here is what the resource editor shows:
            Resource
            The call to Q_INIT_RESOURCE with the base name of the .qrc file, not in any namespace:

            static void InitQrcResources()
            {
            	static bool init = false;
            	if (!init) {
            		init = true;
            		Q_INIT_RESOURCE(QtResource);
            	}
            }
            

            I experimented, but here I am assuming Q_INIT_RESOURCE has to be called once but no more. Then the code that attempts to access the resources

            InitQrcResources();
            	QListWidgetItem* item = new QListWidgetItem;
            [...]
            		QPixmap pixActiveFromQrc(":/DrivenController/Images/check.png");
            		// pixActiveFromQrc.isNull() is true
            		QPixmap pixActiveFromFile("C:\\Images\\check.png");
            		// pixActiveFromFile.isNull() is false
            		QIcon activeIcon(":/DrivenController/Images/check.png");
            		item->setIcon(activeIcon);
            		// no icon on the listWidgetItem
            

            Building...

            1>Rcc'ing QtResource.qrc...
            1>qrc_QtResource.cpp
            1>DrivenController.vcxproj -> C:\...\Plugins\DrivenController.dll
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Did you read the section about the namespace:
              "Note: As the resource initializers generated by rcc are declared in the global namespace, your calls to Q_INIT_RESOURCE() also need to be done outside of any namespace."
              Does it work when you link the resource directly to your app / main.cpp`?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              N 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                Did you read the section about the namespace:
                "Note: As the resource initializers generated by rcc are declared in the global namespace, your calls to Q_INIT_RESOURCE() also need to be done outside of any namespace."
                Does it work when you link the resource directly to your app / main.cpp`?

                N Offline
                N Offline
                Nicolas Leonard
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                Did you read the section about the namespace?
                I had read it, and noted it in my message.

                I didn't believe I was in any namespace, since I don't have any namespace declaration in my library, but just in case I tested with this snipet of code, and... function InitQrcResources() is not in any namespace.

                template <class A, class B>
                struct AreSame { enum { VALUE = -1 }; };
                template <class A>
                struct AreSame<A, A> { enum { VALUE = 1 }; };
                
                struct TestGlobalNamespace
                {
                	int test_namespace[AreSame<TestGlobalNamespace, ::TestGlobalNamespace>::VALUE];
                };
                
                

                I really don't know what more I could do to make sure I am not in any namespace.

                Finally, I don't have access to the app code, I am developing a plug-in as a dynamic link library. Is it possible that the app somehow locks the Qt resources? Is there a specific point in time where that INIT call has to be done? That Qt INIT call just pulls the transfer of the resource file content into the Qt file system?

                Does the presence of standard windows resource (.rc) interfere with Qt resources?

                I have really run out of ideas

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  I would suggest you to start with a simple example of a library which provides qt resources and see if it works. If not then please post the example code here so we can take a look.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Bonnie
                    wrote on last edited by
                    #9

                    FYI, I've tested with a small dll project in QtCreator.
                    Reading resources from a qrc file which is added to the same project doesn't need to call Q_INIT_RESOURCE at all.

                    N 1 Reply Last reply
                    0
                    • B Bonnie

                      FYI, I've tested with a small dll project in QtCreator.
                      Reading resources from a qrc file which is added to the same project doesn't need to call Q_INIT_RESOURCE at all.

                      N Offline
                      N Offline
                      Nicolas Leonard
                      wrote on last edited by
                      #10

                      For anyone interested: the app and my dll were using different qt library versions.

                      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