0

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)

Community
  • 1
  • 1
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • 1
    I'm not sure but I think you should draw a `QStyle::PE_FrameDockWidget` instead of `QStyle::PE_Widget`. Give it a try and check. – Iuliu Nov 24 '14 at 12:28

1 Answers1

2

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

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Strange, I do not notice any difference whether I call the parent function or not ... – Horst Walter Nov 24 '14 at 17:10
  • @HorstWalter Hmmm... you may be safe then? I'm having trouble tracking down exactly what this code legitimately does... – Jonathan Mee Nov 24 '14 at 17:19
  • it allows to use style sheets with derived classes (see update in question if you are interested). How it works en detail I cannot tell, it is the common code required to do so (found on Qt site as linked above). Without you cannot use style sheet selectors for derived classes – Horst Walter Nov 24 '14 at 18:13