简体   繁体   English

QLabel从文件加载图像

[英]QLabel load image from file

I use Qt Resource System to load images. 我使用Qt资源系统加载图像。

But Resource Collection Files (.qrc) only 20MB 但是资源收集文件(.qrc)仅20MB

So I try to use QImage::loadFromData to load image for my application to use. 所以我尝试使用QImage :: loadFromData加载图像供我的应用程序使用。

But for Resource Collection Files (.qrc) I use 但是对于资源收集文件(.qrc),我使用

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

to load image . 加载图像。

How to load image with QImage::loadFromData 如何使用QImage::loadFromData加载图像

How to use relative path for qrc? 如何为qrc使用相对路径?

And I can't compile and update the terminal.qrc. 而且我无法编译和更新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 这是一个使用loadFromData加载到QPixmap的简单示例。您可以以相同的方式加载到qimage,但无论如何都必须将其转换为pixmap才能加载到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: 通常,通常将image.png文件放在* .qrc所在的目录中,例如:

${Project_Resource_Directory}/images/dashboard_n.png

and the .qrc file would be placed here: .qrc文件将放在这里:

${Project_Resource_Directory}/resources.qrc

your .qrc file should look like this: 您的.qrc文件应如下所示:

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

put this code in your .pro file: 将此代码放在您的.pro文件中:

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 在项目结构中替换资源目录名称,而不是$ {Project_Resource_Directory},以便Qt可以检测到您的* .qrc文件并将其编译为使用rcc的c代码

then you can just use this address in any of your cpp codes using this: 那么您可以使用以下代码在任何cpp代码中使用该地址:

":/images/dashboard_n.png"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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