PDA

View Full Version : label - pixmap - problem



qwrhiobasdbgghoasdf
1st December 2009, 18:44
hallo

i m new to qt, and i ve got a problem. so i m using qt creator and if im going there in the designer and create a label i have the option to load a pixmap so that the label should show it. so you can browse, select the pic u want ... all great.... even the preview is showing the picture.. but when i run the code nothing is to be seen. only the gray background is visible. why is that?

scascio
1st December 2009, 20:18
Maybe the file you are loading is missing as a resource defined in your .qrc

qwrhiobasdbgghoasdf
1st December 2009, 23:45
yeah, i was thinking the same thing. but then again, why would the source be wrong if i used the build in browser... and why should the preview know where to find it but not the code.... dosn t make much sense.. does it?... mmmm..... maybe i should put some code in just to make things a little clearer..



#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel();
label->setPixmap(QPixmap(QString::fromUtf8("bild.bmp")));
label->show();
return app.exec();
}


so now what i did is... i wrote this little code only to show the pic. and the line with the setPixmap i just took out of the file which was createt by the desiner..... what happens when i run it on my mashine is... i m seeing just the grey background of the mainwindow.. and not the picture..... which i did put in the directory of the project....

scascio
2nd December 2009, 08:42
I dont think that setting file in the designer will add it as a resource. I think it just sets a property. Please correct me if I am wrong

So, either the file is external and need to be found at execution, etiher it is embedded as a resource. In that last approach, you can add it in your .qrc first, then get access to it by prefixing name with ":" and a resource prefix like that ":/prefixInQrc/pathTotheFile"

Anyway, you can check if file exists while QFileInfo::exists and if pixmap has been loaded with QPixmap::isNull or return value of QPixmap::load

qwrhiobasdbgghoasdf
2nd December 2009, 10:41
hi,
i tried the approach using the qt resource system u mentioned and it worked instantly. oh yeah, i also gave the isnull fn a try... also very useful...
so thx a million 4 ur help

oh well yeah.. if someone else is ever going to have the same problem.... this way u can display the pic...



#include <QApplication>
#include <QLabel>
#include <QPixmap>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QLabel *label = new QLabel();
QPixmap image(":/bild.bmp");
label->setPixmap(image);
if(image.isNull()){
label->setText("null pic");
}
label->show();
return app.exec();

}


but u need to create a resource file and mention there ur picture.... in my case... bild.bmp... oh yeah.... and also u need to mention ur resource file in your project file... sounds really more depressing than it is.... if u use qt creator.... things are even done for u... just create a new res file... and select there the stuff u want to have.... then u can just use it the :/ way shown in the code... the rest ll be done for u...
enjoy

didier
16th September 2010, 00:29
Not to stay passive, I've decided to link my executable with debug versions of Qt libs.
I hope discovering why image loading is unsuccessful.

I keep informed the forum.

Thank you all for your suggestions. It permits at least to eliminate tracks and to discriminate the causes.