PDA

View Full Version : Adding image to QLabel using QPixmap



jvalerio
15th December 2014, 17:10
Hello,

I'm trying to add a label as an image. So I've created a resource under Qt and added in the images, but when I try to run the code it only adds in the image if I give it the extact path and not the short version.

For example:

This works

void MainWindow::TestIOinputs()
{
QPixmap stat_GO ("C:/Qt/MyProject/images/stat_noGO.png");
ui->label_3->setPixmap(stat_GO);
}


This doesn't work

void MainWindow::TestIOinputs()
{
QPixmap stat_GO (":/images/stat_noGO.png");
ui->label_3->setPixmap(stat_GO);
}


The resource file is called images and I have added the images there. I can also see that the .pro file has the resources.qrc added in. Any ideas?

wysota
15th December 2014, 17:33
Make sure the prefix is the same as you set it in the resource file. If you are using Qt Creator you can right click the image file in the list of files and copy its path to clipboard.