Enable own widget for stylesheet

霸气de小男生 提交于 2019-12-25 01:15:36

问题


I want to enable my own widget class for stylesheets, whereat I am not talking about setStyleSheet(qss), but selectors in a qss stylesheet. It is understood that I have to replace the "::" with "--" in namespaces.

Here ( Qt Stylesheet for custom widget ) I have found a similar question, but it is > 4 years old. Based on the answer I have some detailed questions:

a) Is the published approach with an overridden paintEvent still valid (Qt5.6/5.7), from https://stackoverflow.com/a/8817908/356726

void CustomWidget::paintEvent(QPaintEvent *)
 {
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
 }

b) In the same thread ( https://stackoverflow.com/a/22094319/356726 ) it is said, I do not need to override paintEvent. Ok, leads me to: b1) is it harmful to override paintEvent anyway, even with QFrame? b2) What is with other base classes, e.g. QTableView? What makes QFrame having this particular role?

c) has anybody found an official Qt documentation on that topic. Nice code in a, but where does it come from? (here) Honestly I do not understand what it does.

-- Edit --

Daniel has pointed out the source of that magical paintEvent snippet here (QWidget paragraph). Interesting that the same ("supports only ..") is said for QDialog, which could mean I have to use the snippet there as well. I fail to understand why they do not add that snippet to the paintEvent of QWidgetas per default.


回答1:


Is the published approach with an overridden paintEvent still valid (Qt5.6/5.7)

  • Yes

In the same thread ( https://stackoverflow.com/a/22094319/356726 ) it is said, I do not need to override paintEvent. Ok, leads me to: b1) is it harmful to override paintEvent anyway, even with QFrame? b2) What is with other base classes, e.g. QTableView? What makes QFrame having this particular role?

  • If you subclass QFrame, it provides its own paint event. QFrame is not a special case, it applies to all widgets. The default QWidget::paintEvent does nothing. It's empty. That is why you have to override it and provide your own painting to enable stylesheets when subclassing QWidget. It is not harmful to override QFrame's paint event, but you will lose the default behavior unless you call the QFrames implementation.

Has anybody found an official Qt documentation on that topic. Nice code in a, but where does it come from?

  • Here are the official docs. If you scroll down a little it states: QWidget Supports only the background, background-clip and background-origin properties. If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:

-

void CustomWidget::paintEvent(QPaintEvent *)    
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}


来源:https://stackoverflow.com/questions/37952348/enable-own-widget-for-stylesheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!