Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

QPixmap pixmap = QPixmap (":/logo.png");
ui->label->setPixmap( QPixmap(pixmap));

Project.pro

OTHER_FILES += \
    logo.png

RESOURCES += \
    Resources.qrc
like image 410
Nicholas Post Avatar asked Jul 29 '14 15:07

Nicholas Post


People also ask

What is QRC file in Qt?

Compiled-In Resources qmake will produce make rules to generate a file called qrc_application. cpp that is linked into the application. This file contains all the data for the images and other resources as static C++ arrays of compressed binary data.

How do I add a QRC file to QTC?

Select Add to create a . qrc file and to open it in the Qt Resource Editor. To add resources to the file, select Add > Add Files. In the Prefix field, you can change the prefix.


1 Answers

After countless hours of trial, error, and mostly frustration, I have been able to resolve this issue by simply running qmake.

To do this I just right clicked on the project and clicked "Run qmake" and suddenly the image errors (as well as a few others) were resolved!

like image 148
Nicholas Post Avatar answered Sep 25 '22 22:09

Nicholas Post