PDA

View Full Version : Can't get pixmap to show up in QGraphicsView



mg444
4th January 2011, 15:47
Hello -

For some reason I can't get a pixmap to show up in a QGraphicsView on my form. I am using Qt 4.7.1. Here is my code:

QPixmap pixMap("C:/Stuff/Projects/myImage.jpg");

QGraphicsScene scene;
scene.addPixmap(pixMap);
ui.graphicsView->setScene(&scene);
ui.graphicsView->show();

What am I missing / doing wrong?

Thanks.

Lykurg
4th January 2011, 15:54
Can you show jpeg's at all? E.g. on a QLabel? Do you have the requires image plugins?

Also do you see anything on the view? Because I guess it is because you create the scene on the stack, not on the heap... That would be a basic C++ error.

mg444
4th January 2011, 16:05
Yep, you are right...duh. My scene was being deleted. All is good now. Thanks.

Nahla
17th April 2011, 13:58
Dear friends , i can't understand clearly what is meant by image plugins or what you mean by you declared the scene in the stack?

i have the same problem and the image is not displayed
thanks

jano_alex_es
18th April 2011, 08:44
"In the stack" means you are creating the scene statically.

Basically:

QGraphicsScene scene;

instead of

QGraphicsScene scene = new QGraphicsScene();

If you write it without the "new" operator, it's deleted when it reachs the end of the scope (the end of the function, in this case) So you are creating a scene, adding a QGraphicsPixmapItem and deleting the whole scene when the function ends.
If you use the second option the scene is stored in the dinamic memory, so it's not deleted.

If it does not help you, please provide some code.