开发者

Qt: Using QPainter on child widgets

开发者 https://www.devze.com 2022-12-10 00:16 出处:网络
I\'m having a Qt/C++ problem with a simple QWidget program that draws an ellipse inside a child QWidget.

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

开发者_如何学Python

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.


First thing I'd try would be to install an event filter on the child widget (see QObject::installeEventFilter()), then, in the parent widget, grap the QEvent::Paint event, and do the painting there.

Where you create the 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;
}


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().


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

0

精彩评论

暂无评论...
验证码 换一张
取 消