Nyaruko
Nyaruko

Reputation: 4479

How to stop a paint event from propagating?

I have two widgets, A and B, A has B as its parent.

Inside the A widget, I have a timer to trigger the repaint slot of itself. Thus, the paintEvent of widget A is triggered. However, I found B's paintEvent is also triggered. How could I trigger only A's paintevet?

I have tried to accept A's paintEvent as:

void A::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    paintA();
    event->accept();
}

But it doesn't help. What should I do?

Upvotes: 1

Views: 1130

Answers (2)

jonspaceharper
jonspaceharper

Reputation: 4367

When a widget is sent a paint event, so are all of its enabled children. You can work around this by installing an event filter on the child widget and discarding any paint events you don't want.

Upvotes: 2

user362515
user362515

Reputation: 917

You can't because Qt must do composition of the widgets.

Options:

  • Consider making the widget a non-child and display it as a separate "window". You can use Qt::FramelessWindowHint and Qt::WA_TranslucentBackground to make it look as a child widget. This option will give you perfect results as it leaves the composition to the underlying OS, which, at least on Desktop, will not repaint the bottom widget unless requested.

  • Consider caching. Use QPixmapCache to cache all drawing of your bottom widget to one window-sized pixmap, which will be very fast to draw when needed.

Upvotes: 1

Related Questions