views:

292

answers:

2

I'm having a QT/C++ problem with a simple QWidget program that draws an ellipse inside a child QWidget.

The program is composed of:
(1) A parent QWidget
(2) A child QWidget (used as the drawing surface for an ellipse)
(3) A draw QPushButton

Here is part of the code (QPushButton Slot and Signal code 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); doesn't do anything. The program draws the ellipse only if I replace line 2 with painter.begin(this); but that draws on the parent QWidget and not on the child QWidget as desired.

p.s. I have the child_Widget housed inside a GroupBox which in turn is housed inside a QVBoxLayout Any Suggestion?

Thanks.

+2  A: 

As stated in the QPainter documentation

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.

If you want to draw on that widget you will need to do so from it's own paintEvent().

linuxman
linuxman, how can I accomplish that? I originally had the child widget in its own class as a render area that was instantiated inside the parent widget class, but I eventually merged the two in one class b/c I needed the render area to access members of the parent widget class, and using the friend keyword didn't work.Also is there another device (besides QWidget) to which I can draw the ellipse w/out using QPainter?
Ben E
By subclassing it and reimplement it's paintEvent() function.
linuxman
+2  A: 
Fred