Qt: using QPainter in child widgets

I have a problem with Qt / C ++ with a simple QWidget program that draws an ellipse inside a child QWidget .

The program consists of:
(1) Parent of QWidget
(2) QWidget Child (used as a drawing surface for an ellipse)
(3) Draw QPushButton

Here is a piece of code (QPushButton Slot and Signal code are omitted for simplicity)

 void Draw::paintEvent(QPaintEvent *event) { QPainter painter; painter.begin(child_Widget); //The line with the problem painter.setRenderHint(QPainter::Antialiasing, true); painter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap)); painter.setBrush(QBrush(Qt::green, Qt::SolidPattern)); painter.drawEllipse(50, 50, 100, 100); painter.end();} 

Line 2 painter.begin(child_Widget); doing nothing. The program draws an ellipse only if I replace line 2 with painter.begin(this); , but painted on the parent QWidget , and not on the child QWidget as desired.

PS I have a child_Widget hosted inside a GroupBox, which in turn is inside a QVBoxLayout .

Any suggestion?

Thanks.

+4
source share
3 answers

First of all, I will try to set the event filter in the child widget (see QObject :: installeEventFilter () ), then, in the parent widgets, capture the QEvent :: Paint event and draw a picture there.

Where do you create a child widget:

  // ...
     childWidget = new QWidget (this);
     childWidget-> installEventFilter (this);
 // ...

Then in the parent:

  bool Draw :: eventFilter (QObject * watched, QEvent * event)
 {
     if (watched == childWidget && event-> type () == QEvent :: Paint) {
         QPainter painter;
         painter.begin (childWidget);
         painter.setRenderHint (QPainter :: Antialiasing, true);
         painter.setPen (QPen (Qt :: black, 12, Qt :: DashDotLine, Qt :: RoundCap));
         painter.setBrush (QBrush (Qt :: green, Qt :: SolidPattern));
         painter.drawEllipse (50, 50, 100, 100);
         painter.end ();
         return true;  // return true if you do not want to have the child widget paint on its own afterwards, otherwise, return false.
     }
     return false;
 }
+6
source

As indicated in the QPainter documentation

Warning. When paintdesvice is a widget, a QPainter can only be used inside the paintEvent () function or in a function called paintEvent (); if the widget attribute Qt :: WA_PaintOutsidePaintEvent is not set. On Mac OS X and Windows, you can only paint in paintEvent () regardless of this attribute.

If you want to draw this widget, you will need to make it from its own paintEvent ().

+4
source

You can draw on pixmap and draw pixmap in widget draw event. And it can be any function or slot, not necessarily a drawing event, for example. You can have several for drawing different objects. You can draw from anywhere in pixmap, the requirement that the paint event is used only for the widget that will draw pixmap. You can even draw in another stream if it is a complex scene and only update the result from pixmap in the main stream.

+1
source

All Articles