2014-11-24 88 views
0

QDockWidget派生類中我enable style sheet支持如下:在派生類中調用父類的繪畫事件?

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或這是錯的(如果是的話請詳細說明)。在原始code example父函數是不是調用,但我想知道這是否正確?它會錯過任何功能,不是嗎?

注:上述代碼允許如所描述的使用樣式表與派生類: Qt stylesheet in derived class in C++ namespace (selector)

+1

我不知道,但我認爲你應該畫一個'將QStyle :: PE_FrameDockWidget'代替'將QStyle :: PE_Widget'的。試試看並檢查。 – Iuliu 2014-11-24 12:28:40

回答

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

+0

奇怪的是,我沒有注意到我是否調用父函數或沒有任何區別... – 2014-11-24 17:10:01

+0

@HorstWalter嗯...你可能安全嗎?我無法準確跟蹤代碼合法地做了什麼...... – 2014-11-24 17:19:53

+0

它允許使用具有派生類的樣式表(如果您有興趣,請參閱問題更新)。它是如何工作的詳細我不知道,這是這樣做的通用代碼(在上面鏈接的Qt網站上找到)。沒有你不能使用派生類的樣式表選擇器 – 2014-11-24 18:13:53

相關問題