QSS& @pyqtproperty

发布于 2025-02-12 21:41:29 字数 1807 浏览 1 评论 0原文

作为一个简单的例子,假设我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2025-02-19 21:41:30

有关 qss property property selector 说:说::

您可以使用此选择器来测试支持qvariant :: tostring()

的任何QT属性

QSS解析器代码非常复杂且有些隐秘的任何QT属性,而且我从来没有能够完全遵循其逻辑,但是我相信它总是尝试每当包含数字时,将字符串值转换为数字,因此0 QSS属性选择器值将与“ 0”ToString()

通常,始终使用围绕价值的引号:

    self.setStyleSheet('''
        StatefulWidget[state="0"] {
            background-color: red;
        }
        StatefulWidget[state="1"] {
            background-color: green;
        }
    ''')

The documentation about the QSS property selector says:

You may use this selector to test for any Qt property that supports QVariant::toString()

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 from toString().

As a general rule, always use quotes around values:

    self.setStyleSheet('''
        StatefulWidget[state="0"] {
            background-color: red;
        }
        StatefulWidget[state="1"] {
            background-color: green;
        }
    ''')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文