开发者

Qt: Have parent Widget class draw something in addition to the child class widget

开发者 https://www.devze.com 2023-02-12 12:58 出处:网络
More and more this seems like a philosophical Qt question: I have a widget representing a window A. I have a widget representing a window, deriving from A, called B.

More and more this seems like a philosophical Qt question:

I have a widget representing a window A. I have a widget representing a window, deriving from A, called B.

B draws its stuff, but I want A开发者_Python百科 to contribute some drawing, e.g. draw an image OVER what B draws. I want B to be completely unaware of this, not having to cooperate with A other than deriving from it.

Is that possible?


Override event() in A:

bool A::event ( QEvent * e)
{
  bool handled = QWidget::event(e);
  if(e->type() == QEvent::Paint) {
    // cast e to QPaintEvent and do the overlay painting
  }
  return handled;
}


I'm no QT expert, but you could do this in a hacky way by having A delegate its drawing to a child component of identical size located at (0,0). Naturally, the child component would be drawn after your window widget in the hierarchy, and so this would achieve what you're after.

0

精彩评论

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