PDA

View Full Version : QPixmap in GraphicsView ??



WetCode
11th December 2013, 15:01
I have been trying to get a image to show in a GrapichsView.
But after alot of trail and error I have not got it to work.
Would anyone be so kind to point me in the right direction?



QGraphicsScene scene;
ui->graphics_ImgLib->setScene(&scene);

QPixmap image;
image.load(":/IMG/IMG/logo.png");
scene.addPixmap(image);

QGraphicsPixmapItem pixMapItem;
pixMapItem.setPixmap(image);

scene.addItem(&pixMapItem);
ui->graphics_ImgLib->show();


Thanks for any help.

Cheers
WetCode

ChrisW67
11th December 2013, 22:16
What is the scope of the QGraphicsScene instance? My guess is that "scene" is going out of scope immediately or the pixmap is not in the resource file location you think it is.

WetCode
12th December 2013, 00:30
I am wery certen the image is there as i am displaying it several places.
When you say out of scope, do you mean its not within the QGrapichsView ?

ChrisW67
12th December 2013, 02:21
No, I mean scope of time over which the QGraphicsScene object exists. If you create the scene on the stack (as in your example) the object ceases to exist when the variable goes out of scope (i.e. usually at the end of the enclosing { } block). When the scene is destroyed the view has nothing to display, and that's what you see. The QGraphicsScene must exist as long as the view needs to display it.

This is generic C++ and not specific to Qt.