tags:

views:

38

answers:

1

Hello,

I need draw with QPainter, but when i try to do it not in

void paintEvent(QPaintEvent *)

I catch error: QPainter::setPen: Painter not active

QPainter::setBrush: Painter not active

Can i use qpainter not in void paintEvent(QPaintEvent *) ?

Thank you.

+3  A: 

You are using a widget as the paint device for QPainter. In this case, QPainter can usually only be used within the context of the widget's paint event. The QPainter documentation has the following to say about this:

Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.

It is possible to use QPainter outside a paint event by setting another paint device for QPainter, for example a QPixmap.

Ton van den Heuvel
I completely missed the word "not" in this question :)
Arnold Spence
thank you for reply. Than what can i use for drawing in widget from different methods?
shk
You don't need to draw from different methods. Put any code for drawing in the paintEvent(). From different methods you can trigger a repaint by calling update(). If you need to paint something different at some point, have your other methods modify some data or flags that are used in the paintEvent() to change how things are rendered.
Arnold Spence
For example i have 10 rectangles in my window? they are paint with qpainter. Now i want to click in one of the rectangles and in this rect must draw circle.
shk
How can i do it?
shk
It sounds like the Graphics View framework might suit your needs better than drawing in a widget. But for your last question, you could maintain a list or array of bools for each rectangle and when you determine that one of the was clicked on, set the bool for that rectangle to true. In your paintEvent(), look at each bool as you draw each rectangle and include a circle if the bool is true.
Arnold Spence
Yed, it'd good idea. Thank you @Arnold Spence
shk