我想用我的全球qss
樣式與派生類。我知道我必須重寫paintEvent
(style sheet reference或here)。Qt的樣式表在派生類在C++的命名空間(選擇)
void CustomWidget::paintEvent(QPaintEvent *) {
QStyleOption opt;
opt.init(this); // tried initFrom too, same result=>not working
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
但是,它似乎沒有工作。與和下面的樣式表線I面對:
CDerived { background-color: black; } // no effect
QWidget { background-color: black; } // works
CDerived
如上實現paintEvent
。還有什麼我需要做的?
- 編輯/解決方案 -
感謝JK的提示我已經想通了。我上面的例子其實並不正確地反映我的情況。 我的真實課程駐留在一個C++名稱空間中(我的錯誤我錯過了)。所以我必須在qss中寫MyNamespace--CDerived
。請參閱「Widgets inside C++ namespaces」
後,我在這裏試圖JK的簡單的例子,我突然意識到我的錯誤!
正確的:
MyNamespace--CDerived { background-color: black; } // works, use -- for ::
備註:SO Relateds問題(a,b),但沒有回答這個特定問題。我的派生類駐留在C++名稱空間中。
我不知道,如果它的我,但我在這裏找不到'opt.init()':http://qt-project.org/doc/qt-5/qstyleoption.html – msrd0 2014-10-06 19:15:04
另見例子在這裏使用Qt StyleSheets:http://qt-project.org/doc/qt-5/stylesheet-examples.html – msrd0 2014-10-06 19:16:25