为样式表启用自己的小部件 [英] Enable own widget for stylesheet
问题描述
我想为样式表启用我自己的小部件类,在此我不是谈论 setStyleSheet(qss)
,而是 qss 样式表中的选择器.据了解,我必须用--"替换 ::" 在命名空间中.
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.
在这里(自定义小部件的 Qt 样式表)我发现了一个类似的问题,但它已经 > 4 岁了.根据答案,我有一些详细的问题:
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) 已发布的带有覆盖 paintEvent
的方法是否仍然有效 (Qt5.6/5.7),来自 https://stackoverflow.com/a/8817908/356726
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) 在同一线程 ( https://stackoverflow.com/a/22094319/356726 ) 中说,我不需要覆盖 paintEvent
.好的,引导我:b1) 无论如何覆盖 paintEvent
是否有害,即使使用 QFrame
?b2) 与其他基类有什么关系,例如QTableView
?是什么让 QFrame
有这个特殊的作用?
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) 有没有人找到有关该主题的官方 Qt 文档.不错的代码,但它来自哪里?(这里) 老实说,我不明白它的作用.
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.
-- 编辑 --
Daniel 指出了那个神奇的 paintEvent
片段的来源 此处(QWidget
段落).有趣的是,对于 QDialog
来说也是如此(仅支持 ..),这可能意味着我也必须在那里使用代码片段.我不明白为什么他们不默认将该片段添加到 QWidget
的 paintEvent
中.
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 QWidget
as per default.
推荐答案
已发布的带有覆盖paintEvent的方法是否仍然有效(Qt5.6/5.7)
- 是的
在同一个线程(https://stackoverflow.com/a/22094319/356726)中据说,我不需要覆盖paintEvent.好的,让我想到:b1) 无论如何覆盖paintEvent 是否有害,即使使用QFrame?b2) 与其他基类有什么关系,例如QTableView?是什么让 QFrame 有这个特殊的作用?
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?
- 如果您子类化
QFrame
,它提供了自己的绘制事件.QFrame
不是特例,它适用于所有小部件.默认QWidget::paintEvent
什么都不做.它是空的.这就是为什么在子类化QWidget
时必须覆盖它并提供自己的绘画以启用样式表的原因.覆盖QFrame
的绘制事件并无害处,但除非调用QFrame
的实现,否则您将失去默认行为.
- If you subclass
QFrame
, it provides its own paint event.QFrame
is not a special case, it applies to all widgets. The defaultQWidget::paintEvent
does nothing. It's empty. That is why you have to override it and provide your own painting to enable stylesheets when subclassingQWidget
. It is not harmful to overrideQFrame
's paint event, but you will lose the default behavior unless you call theQFrame
s implementation.
有没有人找到有关该主题的官方 Qt 文档.不错的代码,但它来自哪里?
- 这里是官方的文档.如果您向下滚动一点,它会指出:
QWidget
仅支持背景,background-clip 和 background-origin 属性.如果你子类化从 QWidget,您需要为您的自定义提供一个paintEventQWidget如下:
- 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);
}
这篇关于为样式表启用自己的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!