QSS& @pyqtproperty
作为一个简单的例子,假设我有一个qpushbutton
,它使用button.setstylesheet()
对其应用一些基本样式。此外,我希望样式的某些部分(背景颜色)改变对用户触发的事件的响应。
我可以在整个代码中简单地调用button.setStylesHeet()
,但是这些将删除现有的样式属性。因此,我将无法对某些信号进行编辑一个样式属性,而是每次样式更改时都必须指定所有样式属性(即使实际上仅修改了一个或几个样式属性)。
我知道可以使用 qss property property selector < /a>显然可以与一起使用的“任何支持的QT属性Qvariant :: ToString()
“ 。此处也为C ++描述了这一点: dynamic properties&amp;样式表。
因此,假设我有一个自定义小部件类,我希望它的一些样式依赖(响应性地)对我的自定义类的属性,例如state
属性。听起来我应该能够做类似的事情,
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import pyqtProperty
class StatefulWidget(QPushButton):
""" QPushButton colored to reflect its state. """
def __init__(self, state=0):
super().__init__()
self.state = state
self.setStyleSheet('''
StatefulWidget[state=0] {
background-color: red;
}
StatefulWidget[state=1] {
background-color: green;
}
''')
@pyqtProperty(int)
def state(self):
return self._state
@state.setter
def state(self, state):
# Register change of state
self._state = state
# Update displayed style
self.style().polish(self)
但这对我不起作用。
谁能建议做出这项工作的正确方法?预期的行为是,每当属性更改时,应更新样式,但应修复基础CSS/QSS。
相关:
As a simple example, suppose I have a QPushButton
that has some basic styling applied to it using button.setStyleSheet()
. Additionally, I want some parts of the style (background color) to change in response to events triggered by the user.
I could simply make calls to button.setStyleSheet()
throughout my code, but these will erase pre-existing style attributes. So I would not be able to edit just one styling attribute in response to some signal, rather I would have to specify all style attributes every time the style changes (even if only one or a few style attributes are actually being modified).
I understand this can be done more neatly with the QSS property selector which can apparently be used with "any Qt property that supports QVariant::toString()
". This is also described for C++ here: dynamic properties & stylesheets.
So suppose I have a custom widget class, and I want some of its styling to depend (responsively) on a property of my custom class, e.g. a state
property. It sounds like I should be able to do something like
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import pyqtProperty
class StatefulWidget(QPushButton):
""" QPushButton colored to reflect its state. """
def __init__(self, state=0):
super().__init__()
self.state = state
self.setStyleSheet('''
StatefulWidget[state=0] {
background-color: red;
}
StatefulWidget[state=1] {
background-color: green;
}
''')
@pyqtProperty(int)
def state(self):
return self._state
@state.setter
def state(self, state):
# Register change of state
self._state = state
# Update displayed style
self.style().polish(self)
but this does not work for me.
Can anyone suggest the proper way to make this work? The intended behavior is that the style should be updated whenever the property changes, but the underlying CSS/QSS should be fixed.
Related:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有关 qss property property selector 说:说::
QSS解析器代码非常复杂且有些隐秘的任何QT属性,而且我从来没有能够完全遵循其逻辑,但是我相信它总是尝试每当包含数字时,将字符串值转换为数字,因此
0
QSS属性选择器值将与“ 0”
从ToString()
。通常,始终使用围绕价值的引号:
The documentation about the QSS property selector says:
The QSS parser code is quite complex and somehow cryptic, and I've never been able to completely follow its logic, but I believe that it always tries to convert string values to numbers whenever they contain digits, so the
0
value for the QSS property selector will not match the"0"
resulting fromtoString()
.As a general rule, always use quotes around values: