简体   繁体   中英

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.

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);

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM