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. Qt image resource is null
QtWS25 Last Chance

Qt image resource is null

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 5 Posters 4.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.
  • K Offline
    K Offline
    Kelenyche
    wrote on last edited by
    #1

    Hi folks
    It seems like this has been posted and asked a lot, but I've done just about every step I can find, and nothing is working, so I need help!
    I need to set an image as a qt resource, and load it in as the background of a Qchart.

    So here's what I've done:
    Saved the image into a sub-directory in my program.
    created a qrc file named resources.qrc. Here's the contents of that file:

    <RCC>
        <qresource>
        <file>images/scribble.jpg</file>
        </qresource>
    </RCC>
    

    Modified the .pro file with the following contents:

    RESOURCES += extra/resources.qrc
    

    Attempted to load the file with the following code, and done some null checking:

    QImage my_image(":/images/scribble.jpg");
    if(my_image.isNull())
    {
        outputTextBox->append("image is null");
    }
    

    I've also tried:

        QImage my_image;
        my_image.load(":/images/scribble.jpg");
    

    I've completely removed my build folder on the build system and re-initialized the repo and rebuilt the code, but that isn't changing anything either.

    I've confirmed it's finding the resources.qrc by changing both the name and filepath of the qrc file, which throws errors when I run qmake ---.pro
    I've also changed the filepath in the code, i.e. QImage my_image(":/images/scrible.jpg"); or QImage my_image(":/imag/scribble.jpg"); which results in Qt throwing an error on the command line that says the image is not found at runtime. (note the intentional spelling errors)

    So it seems to me that it's finding the image and qrc file, but despite that, is not able to load it in for some reason.
    What am I doing wrong??

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

      Do you actually add the resource to your application?

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

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

        Hi,

        Can you show the structure of your project ?

        Did you try to load the file from the disk just to check that it loads properly ?

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

        K 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          Do you actually add the resource to your application?

          K Offline
          K Offline
          Kelenyche
          wrote on last edited by
          #4

          @Christian-Ehrlicher
          Yes, it's in a folder, available to the application. Like I mentioned, if I change the filepath or name in the code, it throws an error saying it can't find it - so it does seem to be finding it but isn't reading it somehow?

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Can you show the structure of your project ?

            Did you try to load the file from the disk just to check that it loads properly ?

            K Offline
            K Offline
            Kelenyche
            wrote on last edited by
            #5

            @SGaist

            My project structure is:

            project
            -headers
            *.h
            -sources
            -main.cpp
            *.cpp (file that calls the image is in here)
            -extra
            -resources.qrc
            -images
            -scribble.jpg

            I think that's all the relevant info on my structure. What's a way to load the file from disk to see that my program sees it?

            K 1 Reply Last reply
            0
            • K Kelenyche

              @SGaist

              My project structure is:

              project
              -headers
              *.h
              -sources
              -main.cpp
              *.cpp (file that calls the image is in here)
              -extra
              -resources.qrc
              -images
              -scribble.jpg

              I think that's all the relevant info on my structure. What's a way to load the file from disk to see that my program sees it?

              K Offline
              K Offline
              Kelenyche
              wrote on last edited by
              #6

              @Kelenyche said in Qt image resource is null:

              @SGaist

              My project structure is:

              project
              -headers
              *.h
              -sources
              -main.cpp
              *.cpp (file that calls the image is in here)
              -extra
              -resources.qrc
              -images
              -scribble.jpg

              I think that's all the relevant info on my structure. What's a way to load the file from disk to see that my program sees it?

              Oh none of my nice spacing came through lol.

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

                Try editing it with coding tags.

                For the test, just use the full path to the file on the disk.

                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
                • K Offline
                  K Offline
                  Kelenyche
                  wrote on last edited by
                  #8
                   project
                       headers
                           *.h
                       sources
                           main.cpp
                           *.cpp (file that calls the image is in here)
                       extra
                           resources.qrc
                           images
                               scribble.jpg
                  

                  I'll try the full disk path.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    Kelenyche
                    wrote on last edited by
                    #9

                    Full disk path is also coming up with a null image.

                    Fwiw, I'm doing this on a raspberry pi, is it possible there's some permission problems?

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

                      Next step: start your application with the QT_DEBUG_PLUGINS environment variable set to 1 to see what's going on with the image plugins.

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

                      K 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Next step: start your application with the QT_DEBUG_PLUGINS environment variable set to 1 to see what's going on with the image plugins.

                        K Offline
                        K Offline
                        Kelenyche
                        wrote on last edited by
                        #11

                        @SGaist
                        Where should this be set? I'm also doing all this off the command line, no qt creator. Would this be in the .pro file, or an argument called with qmake program.pro?

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

                          On the command line directly

                          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
                          • K Offline
                            K Offline
                            Kelenyche
                            wrote on last edited by
                            #13

                            Ah, ok so I ran:
                            QT_DEBUG_PLUGINS=1 ./program
                            and it dumped a bunch of info. I'm not seeing any problems or failures in here...?

                            Got keys from plugin meta data ("ico", "cur")
                            QFactoryLoader::QFactoryLoader() looking at "/usr/lib/aarch64-linux-gnu/qt5/plugins/imageformats/libqjpeg.so"
                            Found metadata in lib /usr/lib/aarch64-linux-gnu/qt5/plugins/imageformats/libqjpeg.so, metadata=
                            {
                                "IID": "org.qt-project.Qt.QImageIOHandlerFactoryInterface",
                                "MetaData": {
                                    "Keys": [
                                        "jpg",
                                        "jpeg"
                                    ],
                                    "MimeTypes": [
                                        "image/jpeg",
                                        "image/jpeg"
                                    ]
                                },
                                "className": "QJpegPlugin",
                                "debug": false,
                                "version": 330499
                            }
                            

                            This seems like the most relevant portion, but again it seems to have succeeded in recognizing jpgs, so I don't see a problem. Let me know if there's anything else I should be looking for. I'm not seeing parts that are specific to my code, it just looks like Qt's background stuff for loading certain plugins.

                            K 1 Reply Last reply
                            0
                            • K Kelenyche

                              Ah, ok so I ran:
                              QT_DEBUG_PLUGINS=1 ./program
                              and it dumped a bunch of info. I'm not seeing any problems or failures in here...?

                              Got keys from plugin meta data ("ico", "cur")
                              QFactoryLoader::QFactoryLoader() looking at "/usr/lib/aarch64-linux-gnu/qt5/plugins/imageformats/libqjpeg.so"
                              Found metadata in lib /usr/lib/aarch64-linux-gnu/qt5/plugins/imageformats/libqjpeg.so, metadata=
                              {
                                  "IID": "org.qt-project.Qt.QImageIOHandlerFactoryInterface",
                                  "MetaData": {
                                      "Keys": [
                                          "jpg",
                                          "jpeg"
                                      ],
                                      "MimeTypes": [
                                          "image/jpeg",
                                          "image/jpeg"
                                      ]
                                  },
                                  "className": "QJpegPlugin",
                                  "debug": false,
                                  "version": 330499
                              }
                              

                              This seems like the most relevant portion, but again it seems to have succeeded in recognizing jpgs, so I don't see a problem. Let me know if there's anything else I should be looking for. I'm not seeing parts that are specific to my code, it just looks like Qt's background stuff for loading certain plugins.

                              K Offline
                              K Offline
                              Kelenyche
                              wrote on last edited by
                              #14

                              @Kelenyche said in Qt image resource is null:

                              Ah, ok so I ran:
                              QT_DEBUG_PLUGINS=1 ./program
                              and it dumped a bunch of info. I'm not seeing any problems or failures in here...?

                              Got keys from plugin meta data ("ico", "cur")
                              QFactoryLoader::QFactoryLoader() looking at "/usr/lib/aarch64-linux-gnu/qt5/plugins/imageformats/libqjpeg.so"
                              Found metadata in lib /usr/lib/aarch64-linux-gnu/qt5/plugins/imageformats/libqjpeg.so, metadata=
                              {
                                  "IID": "org.qt-project.Qt.QImageIOHandlerFactoryInterface",
                                  "MetaData": {
                                      "Keys": [
                                          "jpg",
                                          "jpeg"
                                      ],
                                      "MimeTypes": [
                                          "image/jpeg",
                                          "image/jpeg"
                                      ]
                                  },
                                  "className": "QJpegPlugin",
                                  "debug": false,
                                  "version": 330499
                              }
                              

                              This seems like the most relevant portion, but again it seems to have succeeded in recognizing jpgs, so I don't see a problem. Let me know if there's anything else I should be looking for. I'm not seeing parts that are specific to my code, it just looks like Qt's background stuff for loading certain plugins.

                              This chunk also seems like it recognized that it needs jpgs:

                              Got keys from plugin meta data ("svg", "svgz")
                              QFactoryLoader::QFactoryLoader() checking directory path "/home/pi/Desktop/program/imageformats" ...
                              loaded library "/usr/lib/aarch64-linux-gnu/qt5/plugins/imageformats/libqjpeg.so"
                              QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/aarch64-linux-gnu/qt5/plugins/accessible" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "/home/pi/Desktop/program/accessible" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/aarch64-linux-gnu/qt5/plugins/accessiblebridge" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "/home/pi/Desktop/program/accessiblebridge" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/aarch64-linux-gnu/qt5/plugins/iconengines" ...
                              QFactoryLoader::QFactoryLoader() looking at "/usr/lib/aarch64-linux-gnu/qt5/plugins/iconengines/libqsvgicon.so"
                              Found metadata in lib /usr/lib/aarch64-linux-gnu/qt5/plugins/iconengines/libqsvgicon.so, metadata=
                              {
                                  "IID": "org.qt-project.Qt.QIconEngineFactoryInterface",
                                  "MetaData": {
                                      "Keys": [
                                          "svg",
                                          "svgz",
                                          "svg.gz"
                                      ]
                                  },
                                  "className": "QSvgIconPlugin",
                                  "debug": false,
                                  "version": 330499
                              }
                              
                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                That part looks good.
                                And I am guessing that it also says somewhere that the plugin was successfully loaded ?

                                The next step: did you check that the file is a valid jpeg ?

                                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
                                • K Offline
                                  K Offline
                                  Kelenyche
                                  wrote on last edited by
                                  #16

                                  Yea I can't find any problems in that debug info, no failures or anything.

                                  I actually created the jpg in paint, it's literally just some scribbles. I have a .png I've been trying to use but no luck, so I wanted to try a different file type, with a differently formatted name to see if anything would change. I don't see how it wouldn't be a valid jpg, having just saved it from paint?

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

                                    Getting really strange...

                                    No special characters in the path ? Not that it should play a role but who knows...

                                    If you use QFile to open it, does it work ?

                                    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
                                    • K Offline
                                      K Offline
                                      Kelenyche
                                      wrote on last edited by
                                      #18

                                      Nope, the full path is: "/home/pi/Desktop/program/extra/images/scribble.jpg"

                                      Alright I'm not familiar with QFile, but I used:

                                      QFile testfile("/home/pi/Desktop/program/extra/images/scribble.jpg");
                                      
                                      if(testfile.exists())
                                      {
                                          outputTextBox->append("opened the file");
                                      }
                                      

                                      and it seems to have succeeded.
                                      will testfile.exists() do the same test as isNull in QImage?

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

                                        No, it just confirms the existence of the file not whether it is valid.

                                        Use the QImageReader class. It's lower level but may give you more information about what is going wrong.

                                        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
                                        • K Offline
                                          K Offline
                                          Kelenyche
                                          wrote on last edited by
                                          #20

                                          Ok so I have:

                                          QImage testimage(64,64,QImage::Format_RGB32);
                                          QImageReader reader("/home/pi/Desktop/program/extra/images/scribble.jpg");
                                          
                                          if(reader.read(&testimage))
                                          {
                                              outputTextBox->append("opened the file");
                                          }
                                          

                                          It's telling me the file is good now...

                                          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