[Solved] PNG Pixmap in QLabel Won't Show



  • I am a noob with Qt/C++. I am loading a transparent PNG in the Pixmap property of a QLabel widget. I can see it in QtCreator, but when I choose Build > Run, it does not display. The image is in the same directory as the project. When I was working on the Pixmap property, I clicked the popdown menu that let me select a file. I selected this local file and it showed up in the label -- but only in QtCreator, not when I run the application. Is this a known bug in Qt5.5 Open Source (Free) version or am I doing something wrong? BTW, I'm on a Mac Air.



  • @maximo

    Hi there,

    I use this code to set Pixmap on my QLabel. Seems to work for me!

    QPixmap pixmapTarget = QPixmap(":/image/icon/target");
    pixmapTarget = pixmapTarget.scaled(size-5, size-5, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image_power->setPixmap(pixmapTarget );
    

    Have a great day,
    Max



  • @maximus Thanks. It appears that this must be a bug in QtCreator. I was able to load the image properly by adding the following right after my ui->setupUi(this);line:

    ui->lblLogo->setPixmap(QPixmap("/Users/maximo/Projects/proj1/logo.png"));
    

    I had to use an absolute path. Is there any way that I can load this from a relative path from the application's own directory?



  • @maximo

    Yes, check the Resource system, really useful, you can set your image in there and it will be bundled with your executable
    Good luck!



  • @maximus Aha! Now I get it. I rightclicked the Projects > Sources in QtCreator, chose Add New... > Qt > Qt Resource File. I gave it a name of simply "res" (and it adds the extension .qrc on the end) and then saw a new Resources folder on the left underneath Forms in the QtCreator project editor. Then, in the resource editor, I clicked the Add popdown listbox and chose Files. I selected my file from the local application directory, although I guess I could have put it in an images folder in that application directory. Then, I went to my label control, cleared out the pixmap property, and chose the Add Resource option, and selected the resource (a little tricky, but I clicked around and found it). I then commented out my pixmap code that I had added in the main window because now I'm doing it the resource way. I then chose Build > Run and it worked! :)



  • Glad it worked! You can add [Solved] - in the title of this post to mark it has solved ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.