Search code examples
c++qtqwidget

Draw on custom widget after it's drawing


I have a custom widget, which inherits QWidget. It has own paintEvent and I cannot change it. So I want to use such a widget in my dialog object, but I need to draw some graphics on it after it draws its own graphics (that widget draws video frames on it, an I need to draw some lines over it). Can I draw every time after the paintEvent of that widget? I used installEventFilter and caught the event wuth type Qt::Paint, but I canoont see anything I've drown. Is there any other way?


Solution

    1. You can derive from the custom widget class, reimplement paintEvent, and call the inherited paintEvent first, then do your drawing.

    2. You can install an event filter on the widget and do the same: call the widget's paintEvent first, then do your drawing.

    3. Hide the other widget. Create your own widget, and call the other widget's render method in your widget's paintEvent, then do your drawing. Since the other widget is presumably rendering video frames that change periodically over time, you might need to use a timer to update() your widget.

    In neither case are you modifying the 3rd party custom widget.

    In order to call other widget's protected paintEvent you need to be using a QWidget, even if just a dummy, invisible one.