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.1k 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.
  • Z Offline
    Z Offline
    zhongdongy
    wrote on 10 Mar 2022, 03:31 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
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 10 Mar 2022, 06:05 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 10 Mar 2022, 12:27
      1
      • C Christian Ehrlicher
        10 Mar 2022, 06:05

        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 10 Mar 2022, 12:27 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.

        A 1 Reply Last reply 10 Mar 2022, 12:34
        0
        • Z zhongdongy
          10 Mar 2022, 12: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.

          A Offline
          A Offline
          artwaw
          wrote on 10 Mar 2022, 12:34 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 10 Mar 2022, 12:37
          0
          • A artwaw
            10 Mar 2022, 12:34

            @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 10 Mar 2022, 12:37 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 10 Mar 2022, 12:47 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
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 10 Mar 2022, 13:14 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 10 Mar 2022, 13:20
                2
                • C Christian Ehrlicher
                  10 Mar 2022, 13:14

                  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.

                  Z Offline
                  Z Offline
                  zhongdongy
                  wrote on 10 Mar 2022, 13:20 last edited by zhongdongy 3 Oct 2022, 13:23
                  #32

                  @Christian-Ehrlicher The custom command was added because I once suspected the CMAKE_AUTORCC was not working correctly. Just now as you suggested, I commented the custom Cmake command, but still no luck.

                  It is very clear now, this command has nothing to do with the QImage not loading data but thank you anyway. It somehow just failed on my laptop, I'll try to start a Linux virtual machine and see how.

                  May I know your Qt version? Mine is Qt 6.2.3.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 10 Mar 2022, 13:32 last edited by
                    #33

                    My Qt version is 5.15.2 on Linux and 6.2.3 on Win (MinGW)

                    Bytes length:  277370
                    qt.gui.icc: fromIccProfile: failed minimal tag size sanity
                    Is QImage instance null:  false
                    qt.gui.icc: fromIccProfile: failed minimal tag size sanity
                    Is QImage2 instance null:  false
                    

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

                    Z 2 Replies Last reply 10 Mar 2022, 13:35
                    0
                    • C Christian Ehrlicher
                      10 Mar 2022, 13:32

                      My Qt version is 5.15.2 on Linux and 6.2.3 on Win (MinGW)

                      Bytes length:  277370
                      qt.gui.icc: fromIccProfile: failed minimal tag size sanity
                      Is QImage instance null:  false
                      qt.gui.icc: fromIccProfile: failed minimal tag size sanity
                      Is QImage2 instance null:  false
                      
                      Z Offline
                      Z Offline
                      zhongdongy
                      wrote on 10 Mar 2022, 13:35 last edited by
                      #34

                      @Christian-Ehrlicher Thank you very much. From what we've seen now, the code itself has no issue. I'll try on Linux, or other versions on Windows and see how. I'll update here if I found something.

                      1 Reply Last reply
                      0
                      • C Christian Ehrlicher
                        10 Mar 2022, 13:32

                        My Qt version is 5.15.2 on Linux and 6.2.3 on Win (MinGW)

                        Bytes length:  277370
                        qt.gui.icc: fromIccProfile: failed minimal tag size sanity
                        Is QImage instance null:  false
                        qt.gui.icc: fromIccProfile: failed minimal tag size sanity
                        Is QImage2 instance null:  false
                        
                        Z Offline
                        Z Offline
                        zhongdongy
                        wrote on 12 Mar 2022, 00:17 last edited by
                        #35

                        @Christian-Ehrlicher Just now I use a Linux (Ubuntu 21.10) vm to test the same code with GCC, same result as you've posted:

                        Warning: Ignoring WAYLAND_DISPLAY on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
                        Bytes length:  277370
                        qt.gui.icc: fromIccProfile: failed minimal tag size sanity
                        Is QImage instance null:  false
                        qt.gui.icc: fromIccProfile: failed minimal tag size sanity
                        Is QImage2 instance null:  false
                        

                        And I've already re-installed Windows 11 (code failed) now using Windows 10 host (also failed). This is really frustrating because I'll have to write Qt on a Windows host and compile it inside Linux vm, lol. I cannot use Linux directly because the Intel Iris Xe graphics card shipped with Intel i7-1195G7 has screen tearing issue on almost all Linux distros (Fedora, Arch, Ubuntu, Debian, RH, Deepin...). Spent the whole day yesterday to install and test...

                        I'll try submit a bug report on bugtracker if none submitted before. Thanks everyone.

                        1 Reply Last reply
                        0

                        34/35

                        10 Mar 2022, 13:35

                        • Login

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