View Full Version : QPixmap and QGridLayout

22nd February 2006, 03:43

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:

QLabel *myimage = new QLabel();

QPixmap pix("cylon.jpg");

QGridLayout *gridLayout = new QGridLayout;
gridLayout->addWidget(myimage, 0, 0);
gridLayout->addWidget(slide, 1, 0);/*a slider*/
gridLayout->addWidget(draw, 0, 1);/*a widget with a QPainter*/
gridLayout->addWidget(quit, 1, 1);/*a quit button*/
gridLayout->setColumnStretch(0, 10);
gridLayout->setColumnStretch(1, 10);

22nd February 2006, 08:29
You are using a relative path to your image. Are you sure the path is right? Check if the pixmap is valid (QPixmap::isNull).

22nd February 2006, 11:07
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.

22nd February 2006, 11:59
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)

22nd February 2006, 12:35
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 /).

22nd February 2006, 13:27
It works! I've change the path to a absoute path, and it works perfectly... Thanks, you saved my life!