Results 1 to 6 of 6

Thread: QPixmap and QGridLayout

  1. #1
    Join Date
    Feb 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPixmap and QGridLayout

    Hi,

    I'm a new Qt user and I try to a small application to display a .jpg image. But the image is never diplayed in my QLabel. I'm using a QGridLayout with Qt 4.1 and linux. Is there someone who can help me?

    Here is my code:
    Qt Code:
    1. QLabel *myimage = new QLabel();
    2. myimage->setBackgroundRole(QPalette::Dark);
    3. myimage->setScaledContents(true);
    4.  
    5. QPixmap pix("cylon.jpg");
    6. myimage->setPixmap(pix);
    7.  
    8. QGridLayout *gridLayout = new QGridLayout;
    9. gridLayout->addWidget(myimage, 0, 0);
    10. gridLayout->addWidget(slide, 1, 0);/*a slider*/
    11. gridLayout->addWidget(draw, 0, 1);/*a widget with a QPainter*/
    12. gridLayout->addWidget(quit, 1, 1);/*a quit button*/
    13. gridLayout->setColumnStretch(0, 10);
    14. gridLayout->setColumnStretch(1, 10);
    15. setLayout(gridLayout);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPixmap and QGridLayout

    You are using a relative path to your image. Are you sure the path is right? Check if the pixmap is valid (QPixmap::isNull).

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPixmap and QGridLayout

    There is nothing wrong with the code. As you can sense from the previous post by wysota, the problem most propably derives from current path being different than where the image file is located in.

    In another words, for the above code to work, the image file must be in the same directory where the application is executed from.

  4. #4
    Join Date
    Feb 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPixmap and QGridLayout

    This code works great, but only when I try to debug it with GDB... If I don't use it, the label stay black... How can make it work without GDB?

    (Thanks for your fast replies)

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPixmap and QGridLayout

    Do you run it from the same directory where your image file resides? What happens if you change the image path to an absolute one (starting from /).

  6. #6
    Join Date
    Feb 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPixmap and QGridLayout

    It works! I've change the path to a absoute path, and it works perfectly... Thanks, you saved my life!

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.