Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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?



  • @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.


  • Lifetime Qt Champion

    Try editing it with coding tags.

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



  •  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.



  • 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?


  • Lifetime Qt Champion

    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.



  • @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?


  • Lifetime Qt Champion

    On the command line directly



  • 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.



  • @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
    }
    

  • Lifetime Qt Champion

    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 ?



  • 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?


  • Lifetime Qt Champion

    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 ?



  • 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?


  • Lifetime Qt Champion

    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.



  • 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...


  • Lifetime Qt Champion

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



  • 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.



  • 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. :)



  • 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!


Log in to reply