0

QDockWidget派生类中,我启用样式表支持,如下所示:

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 
    }

问题:我是否必须调用父类paintEvent或者这是错误的(如果是,请详细说明)。在原始代码示例中,调用父函数,但我想知道这是否正确?它会错过那里的任何功能,不是吗?

备注:上面的代码允许使用带有派生类的样式表,如: Qt stylesheet in derived class in C++ namespace (selector)

4

1 回答 1

2

这就是 QDockWidget 在内部所做的。看起来您的布局管理不会在您当前的代码中发生。我希望您可以通过调整窗口大小或调整布局的类似方法来发现问题。

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

于 2014-11-24T12:39:52.113 回答