Results 1 to 6 of 6

Thread: label - pixmap - problem

  1. #1
    Join Date
    Dec 2009
    Posts
    3
    Platforms
    Windows

    Default label - pixmap - problem

    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?

  2. #2
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: label - pixmap - problem

    Maybe the file you are loading is missing as a resource defined in your .qrc

  3. #3
    Join Date
    Dec 2009
    Posts
    3
    Platforms
    Windows

    Default Re: label - pixmap - problem

    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..

    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6. QLabel *label = new QLabel();
    7. label->setPixmap(QPixmap(QString::fromUtf8("bild.bmp")));
    8. label->show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    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....

  4. #4
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: label - pixmap - problem

    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

  5. #5
    Join Date
    Dec 2009
    Posts
    3
    Platforms
    Windows

    Default Re: label - pixmap - problem

    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...

    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QPixmap>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QLabel *label = new QLabel();
    10. QPixmap image(":/bild.bmp");
    11. label->setPixmap(image);
    12. if(image.isNull()){
    13. label->setText("null pic");
    14. }
    15. label->show();
    16. return app.exec();
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    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

  6. #6
    Join Date
    Sep 2010
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: label - pixmap - problem

    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.

Similar Threads

  1. Replies: 19
    Last Post: 3rd April 2009, 23:17
  2. QWizard::Background Pixmap
    By divya balachandran in forum Installation and Deployment
    Replies: 9
    Last Post: 10th January 2009, 14:40
  3. tool buttons on top of label
    By McKee in forum Qt Programming
    Replies: 5
    Last Post: 2nd November 2008, 16:07
  4. Rotation problem trying to draw a compass widget
    By yellowmat in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2007, 19:03
  5. Replies: 10
    Last Post: 1st February 2006, 10:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.