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

Qt image resource is null

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 5 Posters 5.0k Views 3 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.
  • 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
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #21

                      Within the same application, do you have the same issue if you load a different QImage variable using the usual way ?

                      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
                        #22

                        I think so - I initially tried with the png, then switched to the jpg, and neither one is doing what I expect, so I think they're both coming in null with QImage.

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

                          Ooh, I think I'm onto something. So the way my code was implemented, I read the image, did some resizing and stuff, then was checking if it was null after that, since my textbox wasn't added to the gui yet. I switched to QDebug since that's more handy, and moved that code up - it looks like my resizing stuff was causing the image to become null? I now have a not-resized image that's painting onto the background of the graph!

                          I'll post my full fix once I figure this out. :)

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

                            Yep, this chunk is causing it to become null:

                                int chart_width = static_cast<int>(splineChart->plotArea().width());
                                int chart_height = static_cast<int>(splineChart->plotArea().height());
                                int view_width = static_cast<int>(splineChartView->width());
                                int view_height = static_cast<int>(splineChartView->height());
                                scribble = scribble.scaled(QSize(chart_width, chart_height));
                            

                            So in QImage::scaled, if width or height are zero, it returns a null QImage. For some reason, my width and height are returning 0, so that makes sense. Sorry for the wild goose chase, and thank you for your help!

                            1 Reply Last reply
                            2
                            • Z Offline
                              Z Offline
                              zhongdongy
                              wrote on last edited by
                              #25

                              Sorry to re-open this topic. I recently encountered the same issue. When using QFile to check file size, it returns the correct value. But when I use QImage or QImageReader, the size is always QSize(0, 0) or null.

                              Not like @Kelenyche , I didn't call QImage::scaled() to manipulate the image size.

                              Following @SGaist 's suggestion, I set the QT_DEBUG_PLUGINS=1 environment variable and run the program, the output didn't seem like to provide anything useful:

                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/plugins/platforms" ...
                              QFactoryLoader::QFactoryLoader() looking at "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-debug-visual-
                              studio/plugins/platforms/qwindowsd.dll"
                              Found metadata in lib C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-debug-visual-studio/plugins/platform
                              s/qwindowsd.dll, metadata=
                              {
                                  "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
                                  "MetaData": {
                                      "Keys": [
                                          "windows"
                                      ]
                                  },
                                  "archreq": 1,
                                  "className": "QWindowsIntegrationPlugin",
                                  "debug": true,
                                  "version": 393728
                              }
                              
                              
                              Got keys from plugin meta data QList("windows")
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/platforms" ...
                              loaded library "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-debug-visual-studio/plugins/platforms/qwin
                              dowsd.dll"
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/plugins/platformthemes" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/platformthemes" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/plugins/styles" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/styles" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/plugins/accessible" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/accessible" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/plugins/imageformats" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/imageformats" ...
                              qDebug: background image is null?  true
                              qDebug: background image size?  QSize(0, 0)
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/plugins/accessiblebridge" ...
                              QFactoryLoader::QFactoryLoader() checking directory path "C:/Users/zhongdongy/Workspace/repos/qt-family-hub/cmake-build-
                              debug-visual-studio/accessiblebridge" ...
                              

                              Note: this is a PNG file, so I assume it will not load the QJpeg plugin.

                              The QFile::size() function returns the file size correctly, so the embedded resource definitely exists and loaded.

                              I'm using Qt6 with MSVC (something wrong when linking using mingw) on Windows 11 x64, and font resources have no such issue, only image files.

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

                                There is no Qt png plugin because the support for png is built-in in QtGui so no plugin needed. Therefore it's also not a plugin problem but a problem of your path passed to QFile.

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

                                Z 1 Reply Last reply
                                1
                                • Christian EhrlicherC Christian Ehrlicher

                                  There is no Qt png plugin because the support for png is built-in in QtGui so no plugin needed. Therefore it's also not a plugin problem but a problem of your path passed to QFile.

                                  Z Offline
                                  Z Offline
                                  zhongdongy
                                  wrote on last edited by
                                  #27

                                  @Christian-Ehrlicher Thanks for the response. But the path passed to QFile and QImage or QImageReader are exactly the same. Even when I use the following approach still cannot get a usable QImage instance:

                                    QFile f(":/resources/images/default-background.png");
                                    f.open(QIODevice::OpenModeFlag::ReadOnly);
                                    QByteArray byte_array = f.readAll();
                                    qDebug() <<"Bytes length: " << byte_array.length();
                                    QImage img = QImage::fromData(byte_array);
                                    qDebug() << "Is QImage instance null: " << img.isNull();
                                  

                                  The output:

                                  Bytes length:  277370
                                  Is QImage instance null:  true
                                  

                                  So the path is definitely correct.

                                  artwawA 1 Reply Last reply
                                  0
                                  • Z zhongdongy

                                    @Christian-Ehrlicher Thanks for the response. But the path passed to QFile and QImage or QImageReader are exactly the same. Even when I use the following approach still cannot get a usable QImage instance:

                                      QFile f(":/resources/images/default-background.png");
                                      f.open(QIODevice::OpenModeFlag::ReadOnly);
                                      QByteArray byte_array = f.readAll();
                                      qDebug() <<"Bytes length: " << byte_array.length();
                                      QImage img = QImage::fromData(byte_array);
                                      qDebug() << "Is QImage instance null: " << img.isNull();
                                    

                                    The output:

                                    Bytes length:  277370
                                    Is QImage instance null:  true
                                    

                                    So the path is definitely correct.

                                    artwawA Offline
                                    artwawA Offline
                                    artwaw
                                    wrote on last edited by
                                    #28

                                    @zhongdongy What if you pass the path to the file into QImage constructor? Get rid of the middle man QByteArray.

                                    For more information please re-read.

                                    Kind Regards,
                                    Artur

                                    Z 1 Reply Last reply
                                    0
                                    • artwawA artwaw

                                      @zhongdongy What if you pass the path to the file into QImage constructor? Get rid of the middle man QByteArray.

                                      Z Offline
                                      Z Offline
                                      zhongdongy
                                      wrote on last edited by
                                      #29

                                      @artwaw Thanks for the suggestion. Originally I did use the QImage constructor directly. But it didn't work. So I use QFile to read QByteArray to prove the path is correct.

                                      1 Reply Last reply
                                      0
                                      • Z Offline
                                        Z Offline
                                        zhongdongy
                                        wrote on last edited by
                                        #30

                                        Here is a sample demonstrating the issue I encountered: GitHub | qt-forum-samples/qimage-not-loading. It's based on the reply I posted above. I'm building this project using MSVC, so the CMakeLists.txt file has some extra jobs to copy the debug DLLs.

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

                                          Works fine for me after I removed the strange rcc custom command. You enabled CMAKE_AUTORCC so why to you the call rcc by yourself? Remove it and simply add application.qrc to your list of sources as described in the documentation of CMAKE_AUTORCC.

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

                                          Z 1 Reply Last reply
                                          2

                                          • Login

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