[Solved] QT Resource File - Images Not Showing When Running



  • I just upgraded to QT Creator 5.3 and created a brand new QT Widgets Application project and am using the Microsoft VC++ compiler. All I have is a resource file with "logo.png" added (which opens in QT if I double click it), and a label that I am trying to set the background image for. The problem is that no matter what I do I cannot get the image to show up when the program is running. The only way I can get it to show the image is by not using the resource file and instead map directly to the file (ex. "c:/blah/blah/logo.png")

    Also, if I set the background image in the UI designer, the background shows up in the IDE but NOT when I run the program. I have tried probably 20+ variations of the code, including resource file aliases as well as adding the files to the project directly, and nothing seems to work.

    I'm not sure if there is a step I am missing, or if perhaps there is something that I have to do to get the project to compile the image.

    Resources.qrc
    @
    <RCC>
    <qresource prefix="/">
    <file>logo.png</file>
    </qresource>
    </RCC>
    @

    loginform.cpp
    @
    ui->setupUi(this);
    QPixmap pixmap = QPixmap (":/logo.png");
    ui->label->setPixmap( pixmap);
    @

    Project.pro
    @
    RESOURCES +=
    Resources.qrc

    SOURCES += main.cpp
    loginform.cpp

    HEADERS += loginform.h

    FORMS += loginform.ui

    @



  • The only possibility is that logo.png is not found when you are building the project. My suggestion is to delete the resource file, create new resource file and try.

    Can you check whether pixmap is null ? It will be null if pixmap is not created with given image.

    Also you can try with simple example with one just label and png image. This may give you hint.



  • I put the following code in the project and pixmap is null.
    @
    if (pixmap.isNull())
    MessageBoxA(0,"IS NULL","Error",0);
    @

    I created a new resource file and added the image and it didn't seem to do anything. I also manually edited the resource file to include a file that doesn't exist (logo1.png) and the project built without any errors. Shouldn't it fail it it can't find the file?

    Maybe it has something to do with the VC++ compiler?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Try running qmake when you modify your qrc file. The corresponding C++ file must be regenerated when you modify it.

    Hope it helps



  • I'm relatively new to QT, and C++ in general, so is that something I have to do with a program outside of QT?



  • Ok I think I may have figured it out and it may be the fact that I am a complete noob with QT. There is literally the option to right click on the project and say "run qmake". I did that on my test application and it started displaying the image. I am going to fix up my main app I was working on earlier and see if that same solution fixes that as well. I will let you know the results.



  • Yep, that was it. Just so everyone is aware of the solution, it was simply to right click the project and click "Run qmake".

    Thanks again to all those who helped!


  • Lifetime Qt Champion

    You're welcome !

    Since now you have your qrc file working, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.