1

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?

László Papp
  • 51,870
  • 39
  • 111
  • 135
mmmaaak
  • 803
  • 1
  • 18
  • 38
  • You can override your custom widgets paint event and draw there. Here is an example: http://qt-project.org/doc/qt-4.8/painting-basicdrawing.html – vahancho Sep 25 '13 at 21:01
  • No no, It has it's own implementation of paintEvent, but I cant change it, becouse rendering video frames implemented there, and it's not my code, i use it as dynamic library – mmmaaak Sep 25 '13 at 21:07
  • @mmmaaak: it's != its. I fixed this in your posts, but you keep writing in comments, too. Please understand the difference. It is tiresome to read that... – László Papp Sep 25 '13 at 21:18

2 Answers2

3
  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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
3

This is a very simple code sample that draw inside a custom widget. It draws a blue rectangle inside of a QPushButton.

The method used is exactly what has been described in option 1 by @Kuba

So, you inherit from the custom widget class where you want to draw in, reimplement paintEvent, and call the inherited paintEvent first and the do your drawing.

Hope this helps

#include <QApplication>
#include <QPushButton>
#include <QPainter>
#include <QPaintEvent>

// inherit from the class over which you want to draw
class DrawOverButton : public QPushButton
{
    Q_OBJECT

public:
    DrawOverButton(const QString &text, QWidget *parent = 0) :
        QPushButton(text, parent)
    {
        // enlarge the button so there is some space to draw in
        setStyleSheet("QPushButton {min-height: 60px; "
            "min-width: 120px; margin: 5px;}");
    }

protected:
    virtual void paintEvent(QPaintEvent *event) {
        // call the base class paint event method
        // this will draw the base class content
        QPushButton::paintEvent(event);

        // draw a blue border inside the button
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);
        painter.setPen(QPen(QColor("#3cf"), 4));
        const int distance = 20;
        painter.drawRoundedRect(QRect(distance, distance,
            width() - 2 * distance, height() - 2 * distance),
            10, 10);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    DrawOverButton *button = new DrawOverButton("Button");
    button->show();

    return a.exec();
}

#include "main.moc"