PDA

View Full Version : QGraphicsScene in paintEvent ?



PLan2010
11th June 2010, 15:14
I can create and display a graphics scene, in main(), like this -



QGraphicsScene scene(0, 0, WIDTH, HEIGHT);

Button *button;
Button *button2;

QPixmap bg("/../mypix.png");
scene.addPixmap(bg);

button = new Button();
button2 = new Button();

scene.addItem(button);
scene.addItem(button2);

QGraphicsView view;
view.setScene(&scene);
view.show();


How would I alter the code to put it in a paintEvent function ? Thanks in advance. :confused:

high_flyer
11th June 2010, 15:51
paintEvent() is an event handler, which is called by Qt when a widget needs to paint it self (QPaintEvent).
You do not call event handlers your self, they get called by the event loop.
If you want a widget to have a custom painting, you have to re implement its paintEvent(), but leave the calling to the event loop.

Therefore, your question is "wrong", since paintEvent() never comes by its self, its a member method, which you do not call, just re implement.

PLan2010
11th June 2010, 16:22
I realise that paintEvent() is not directly called and has to be re-implemented - I was wondering if someone could give me example code showing how my example use of a graphics scene construction in main() would look in paintEvent() ?

wysota
11th June 2010, 16:27
Constructing a graphics scene in a paint event doesn't make any sense. Paint event should only contain code for redrawing the widget it is part of.

high_flyer
11th June 2010, 16:29
how my example use of a graphics scene construction in main() would look in paintEvent()
this sentence has not meaning... or I fail to to extract any meaning from it.

Maybe if you explain what it is you want to do, we might help you to get in the right direction.

SneakyPeterson
14th June 2010, 12:16
I also am a little bit confused with what you're trying to do....Are you trying to have a scene embedded in a widget that is recreated every single time the paintevent for the parent widget is called? If that is the case, I believe that in the way qgraphicsscene is currently implemented, all events sent to a parent object are automatically sent to the scene as well, so you shouldn't have to tie the two together. If you are, however, trying to create a brand-new graphics scene every time the paintevent event is called in the eventloop is would urge you to rethink your implementation.