- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我修改了 QGroupBox 的复选框的行为以隐藏/显示组的子项,有效地充当扩展器。它运行良好,但唯一的问题是默认复选框图标看起来像是用于启用/禁用组,而不是展开它。我想用扩展器样式的图标替换它。
我发现这篇文章几乎回答了我的问题(最终我可能不得不使用该解决方案):Change PySide QGroupBox checkbox image .问题是那里提供的答案建议使用自定义复选框图像,而我想使用内置的特定于操作系统的扩展器,比如 QTreeView 中的扩展器,它在我的 PC 上看起来像这样:
这可能吗?扩展器是否被视为程式化的复选框或完全不同的东西?我是 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.cpp 的 drawComplexControl()
函数, CC_GroupBox
案例正在调用
proxy()->drawPrimitive(PE_IndicatorCheckBox, &box, p, widget);
绘制复选框,所以我不明白为什么我重写的函数没有运行。
我不太确定如何进行调试,因为我无法进入 C++ 代码以查看实际调用的内容。谁能提供任何建议来帮助我弄清楚为什么我的重写 drawPrimitive()
没有被调用?
更新 2:
我已经解决了为什么我的覆盖 drawPrimitive()
没有被调用的谜团 - 这是因为我的应用程序使用根级样式表,这导致 QStyleSheetStyle
用作小部件的事件样式。 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_())
更新:
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/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!