python - 将 QGroupBox 复选框视觉效果更改为扩展器

标签 python pyside2

我修改了 QGroupBox 复选框的行为以隐藏/显示组的子项,有效地充当扩展器。它工作得很好,但唯一的问题是默认的复选框图标看起来像是用于启用/禁用该组,而不是扩展它。我想将其替换为扩展器样式的图标。

我发现这篇文章几乎回答了我的问题(最终我可能不得不使用该解决方案):Change PySide QGroupBox checkbox image 。问题是,那里提供的答案建议使用自定义复选框图像,而我想使用内置的特定于操作系统的扩展器,例如 QTreeView 中的扩展器,它在我的电脑上看起来像这样: QTreeView expanders

这可能吗?扩展器是否被视为程式化的复选框或完全是其他东西?我对 Qt 相当陌生,所以我不太熟悉如何处理样式。这是我能够查询的东西吗?如果是的话,它是否与 QCheckBox 样式兼容?除了启用/禁用扩展器之外,我在 QTreeView 文档页面上找不到太多关于扩展器的信息,而且我目前正在尝试挖掘 QTreeView.cpp 源代码以弄清楚它们是如何工作的。

更新

离开 eyllanesc 的答案,我越来越接近解决方案,但我遇到了一些覆盖 QProxyStyle 方法的问题。我正在尝试执行以下操作:

class GroupBoxExpanderStyle(QtWidgets.QProxyStyle):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._replaceCheckboxWithExpander = False

    def drawComplexControl(self, control, option, painter, widget):
        try:
            if control == QtWidgets.QStyle.CC_GroupBox and widget.isCheckable():
                self._replaceCheckboxWithExpander = True
            super().drawComplexControl(control, option, painter, widget)
        finally:
            self._replaceCheckboxWithExpander = False

    def drawPrimitive(self, element, option, painter, widget):
        if element == QtWidgets.QStyle.PE_IndicatorCheckBox and self._replaceCheckboxWithExpander:
            indicatorBranchOption = ... # Set up options for drawing PE_IndicatorBranch
            super().drawPrimitive(QtWidgets.QStyle.PE_IndicatorBranch, indicatorBranchOption, painter, widget)
        else:
            super().drawPrimitive(element, option, painter, widget)

类似的事情似乎已经在this code中完成了。 。我遇到的问题是,我的重写 drawPrimitive() 函数根本没有被 QGroupBox 小部件调用...但它被应用了相同代理样式的其他小部件调用!查看 qcommonstyle.cppdrawComplexControl() 函数,CC_GroupBox 案例正在调用

proxy()->drawPrimitive(PE_IndicatorCheckBox, &box, p, widget);

绘制复选框,所以我不明白为什么我的重写函数没有运行。

我不太确定如何调试它,因为我无法进入 C++ 代码来查看实际调用的内容。谁能提供任何建议来帮助我弄清楚为什么我的重写的 drawPrimitive() 没有被调用?

更新2:

我已经解决了为什么我的重写的 drawPrimitive() 没有被调用的谜团 - 这是因为我的应用程序使用根级样式表,这会导致 QStyleSheetStyle code> 用作小部件的事件样式。 QStyleSheetStyle 直接为 CC_GroupBox 调用自己的 drawPrimitive() 方法,而不是调用 proxy()->drawPrimitive() - 这对我来说似乎是一个错误,事实上 this Qt bug指出样式表与 QProxyStyle 不能很好地混合。我将尝试不再使用样式表。

eyllanesc 的技术适用于 Fusion 风格,因此我接受了他的答案,但它与其他风格不兼容。

最佳答案

一种可能的解决方案是实现 QProxyStyle:

from PySide2 import QtCore, QtGui, QtWidgets


class GroupBoxProxyStyle(QtWidgets.QProxyStyle):
    def subControlRect(self, control, option, subControl, widget):
        ret = super(GroupBoxProxyStyle, self).subControlRect(
            control, option, subControl, widget
        )
        if (
            control == QtWidgets.QStyle.CC_GroupBox
            and subControl == QtWidgets.QStyle.SC_GroupBoxLabel
            and widget.isCheckable()
        ):
            r = self.subControlRect(
                QtWidgets.QStyle.CC_GroupBox,
                option,
                QtWidgets.QStyle.SC_GroupBoxCheckBox,
                widget,
            )
            ret.adjust(r.width(), 0, 0, 0)
        return ret

    def drawComplexControl(self, control, option, painter, widget):
        is_group_box = False
        if control == QtWidgets.QStyle.CC_GroupBox and widget.isCheckable():
            option.subControls &= ~QtWidgets.QStyle.SC_GroupBoxCheckBox
            is_group_box = True
        super(GroupBoxProxyStyle, self).drawComplexControl(
            control, option, painter, widget
        )
        if is_group_box and widget.isCheckable():
            opt = QtWidgets.QStyleOptionViewItem()
            opt.rect = self.proxy().subControlRect(
                QtWidgets.QStyle.CC_GroupBox,
                option,
                QtWidgets.QStyle.SC_GroupBoxCheckBox,
                widget,
            )
            opt.state = QtWidgets.QStyle.State_Children
            opt.state |= (
                QtWidgets.QStyle.State_Open
                if widget.isChecked()
                else QtWidgets.QStyle.State_None
            )
            self.drawPrimitive(
                QtWidgets.QStyle.PE_IndicatorBranch, opt, painter, widget
            )


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    style = GroupBoxProxyStyle(app.style())
    app.setStyle(style)

    w = QtWidgets.QGroupBox(title="Exclusive Radio Buttons")
    w.setCheckable(True)
    vbox = QtWidgets.QVBoxLayout()
    for text in ("Radio button 1", "Radio button 2", "Radio button 3"):
        radiobutton = QtWidgets.QRadioButton(text)
        vbox.addWidget(radiobutton)
    vbox.addStretch(1)
    w.setLayout(vbox)

    w.resize(320, 240)
    w.show()
    sys.exit(app.exec_())

enter image description here

enter image description here

更新:

code 的转换OP给Python提供的内容如下:

from PySide2 import QtCore, QtGui, QtWidgets


class GroupBoxProxyStyle(QtWidgets.QProxyStyle):
    def drawPrimitive(self, element, option, painter, widget):
        if element == QtWidgets.QStyle.PE_IndicatorCheckBox and isinstance(
            widget, QtWidgets.QGroupBox
        ):
            super().drawPrimitive(
                QtWidgets.QStyle.PE_IndicatorArrowDown
                if widget.isChecked()
                else QtWidgets.QStyle.PE_IndicatorArrowRight,
                option,
                painter,
                widget,
            )
        else:
            super().drawPrimitive(element, option, painter, widget)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    style = GroupBoxProxyStyle(app.style())
    app.setStyle(style)

    w = QtWidgets.QGroupBox(title="Exclusive Radio Buttons")
    w.setCheckable(True)
    vbox = QtWidgets.QVBoxLayout()
    for text in ("Radio button 1", "Radio button 2", "Radio button 3"):
        radiobutton = QtWidgets.QRadioButton(text)
        vbox.addWidget(radiobutton)
    vbox.addStretch(1)
    w.setLayout(vbox)

    w.resize(320, 240)
    w.show()
    sys.exit(app.exec_())

关于python - 将 QGroupBox 复选框视觉效果更改为扩展器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55977559/

相关文章:

python - 如何在一行中编写并行循环迭代(列表长度不等)

python - 使用 Python 在 Lambda 中处理 S3 桶触发事件

python - 为什么窗口显示后位置仍然为零?

python - 将 QAbstractListModel 声明为 Pyside2 中的属性

python - 如何让 Qt 异步运行以实现像 Matplotlib ion 模式那样的交互式使用?

Python 模式匹配。匹配 'c[any number of consecutive a' s, b's, or c's or b's, c's, or a's etc.]t'

python - 如何加载预训练的Word2vec模型文件?

python - 标准化 pandas 数据帧每列中的值

python - 如何在 PyQt 中绘制自定义椭圆形?

python - 如何使用 QAbstractItemModel 从 QTreeView 中删除行?