Call parent class' paint event in derived class?

836 views Asked by At

In a QDockWidget derived class I enable style sheet support as follows:

void CDockWidget::paintEvent(QPaintEvent *event)
    {
        QStyleOption opt;
        opt.initFrom(this);
        QPainter p(this);
        this->style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
        // call QDockWidget::paintEvent(event) here ???????
        // I have called QDockWidget::paintEvent(event) here, but did not notice any difference 
    }

Question: Do I have to call the parent class paintEvent or is this wrong (if so please elaborate). In the original code example the parent function is NOT called, but I wonder if this is correct? It would miss any functionality there, wouldn't it?

Remark: The above code allows to use style sheets with derived classes as described in: Qt stylesheet in derived class in C++ namespace (selector)

1

There are 1 answers

3
Jonathan Mee On

This is what the QDockWidget does internally. It looks like your layout management won't happen in your current code. I'd expect that you could see the problem by resizing your window or something similar that would adjust the layout.

void QDockWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QDockWidgetLayout *layout
        = qobject_cast<QDockWidgetLayout*>(this->layout());
    bool customTitleBar = layout->widgetForRole(QDockWidgetLayout::TitleBar) != 0;
    bool nativeDeco = layout->nativeWindowDeco();

    if (!nativeDeco && !customTitleBar) {
        QStylePainter p(this);
        // ### Add PixelMetric to change spacers, so style may show border
        // when not floating.
        if (isFloating()) {
            QStyleOptionFrame framOpt;
            framOpt.init(this);
            p.drawPrimitive(QStyle::PE_FrameDockWidget, framOpt);
        }

        // Title must be painted after the frame, since the areas overlap, and
        // the title may wish to extend out to all sides (eg. XP style)
        QStyleOptionDockWidgetV2 titleOpt;
        initStyleOption(&titleOpt);
        p.drawControl(QStyle::CE_DockWidgetTitle, titleOpt);
    }
}

https://qt.gitorious.org/qt/qt/source/a71e6490b5415f24e38681015ae05326a004a7b7:src/gui/widgets/qdockwidget.cpp#LNaN-NaN