5407
5407

Reputation: 39

QLabel load image from file

I use Qt Resource System to load images.

But Resource Collection Files (.qrc) only 20MB

So I try to use QImage::loadFromData to load image for my application to use.

But for Resource Collection Files (.qrc) I use

QImage image0(":/images/dashboard_n.png");

to load image .

How to load image with QImage::loadFromData

How to use relative path for qrc?

And I can't compile and update the terminal.qrc.

Upvotes: 0

Views: 2226

Answers (2)

Morteza Sherafati
Morteza Sherafati

Reputation: 182

usually it's common to put your image.png file in the directory that your *.qrc is located for example like this:

${Project_Resource_Directory}/images/dashboard_n.png

and the .qrc file would be placed here:

${Project_Resource_Directory}/resources.qrc

your .qrc file should look like this:

<RCC>
     <qresource prefix="/">
         <file>images/dashboard_n.png</file>
     </qresource>
</RCC>

put this code in your .pro file:

RESOURCES += ${Project_Resource_Directory}/resources.qrc

replace your resources directory name in your project structure instead of ${Project_Resource_Directory} so Qt can detect your *.qrc file and compile them to c code using rcc

then you can just use this address in any of your cpp codes using this:

":/images/dashboard_n.png"

Upvotes: 1

Spinkoo
Spinkoo

Reputation: 2080

Here is a simple example to load to a QPixmap using loadFromData.. you can load to a qimage the same way but you will have to convert it to pixmap anyway to load it to a qlabel

 QByteArray *temp = new QByteArray();
 QFile *file = new QFile("image.png");
 file->open(QIODevice::ReadOnly);
 *temp = file->readAll();
 QPixmap *pix = new QPixmap();
 pix->loadFromData(*temp);
 label->setPixmap(*pix);

Upvotes: 1

Related Questions