在 PyQt4 中修改系统样式
Modifying System Style in PyQt4
我正在尝试使启用了三态的 QtCheckBox 在其状态为部分状态时在复选框中绘制一个 X。我目前让 Qt 处理样式,使其与操作系统匹配,我想保持这种方式,只更改复选框的绘制方式。
我一直在尝试通过覆盖 Qt 使用的 QStyle 来做到这一点。我测试了 Qt 默认使用的样式:
>>> app = QtGui.QApplication()
>>> type(app.style())
PyQt4.QtGui.QCommonStyle
但是当我尝试 运行 app.setStyle(QtGui.QCommonStyle())
时,GUI 发生了变化,最明显的是复选标记从 QCheckBox
es 中消失了。
我查阅了 QCommonStyle
,根据文档,它只绘制了所有样式共有的元素。
所以我有两个问题:
- 为什么
type(app.style())
返回 QCommonStyle
,而看起来使用的样式实际上不是 QCommonStyle
?
- 我如何实际获得 Qt 使用的样式?
我正在使用 PyQt4。
Why is type(app.style()) returning QCommonStyle, when it appears that
the style used is actually not QCommonStyle?
像样式一样使用的类一般继承QCommonStyle,例如在我的例子中我有以下样式:
for key in QtGui.QStyleFactory.keys():
st = QtGui.QStyleFactory.create(key)
print(key, st.metaObject().className(), type(app.style()))
输出:
Adwaita-Dark Adwaita::Style <class 'PyQt4.QtGui.QCommonStyle'>
Adwaita Adwaita::Style <class 'PyQt4.QtGui.QCommonStyle'>
Breeze Breeze::Style <class 'PyQt4.QtGui.QCommonStyle'>
bb10dark QBB10DarkStyle <class 'PyQt4.QtGui.QCommonStyle'>
bb10bright QBB10BrightStyle <class 'PyQt4.QtGui.QCommonStyle'>
cleanlooks QCleanlooksStyle <class 'PyQt4.QtGui.QCommonStyle'>
gtk2 QGtkStyle <class 'PyQt4.QtGui.QCommonStyle'>
cde QCDEStyle <class 'PyQt4.QtGui.QCommonStyle'>
motif QMotifStyle <class 'PyQt4.QtGui.QCommonStyle'>
plastique QPlastiqueStyle <class 'PyQt4.QtGui.QCommonStyle'>
qt5ct-style Qt5CTProxyStyle <class 'PyQt4.QtGui.QCommonStyle'>
QtCurve QtCurve::Style <class 'PyQt4.QtGui.QCommonStyle'>
Windows QWindowsStyle <class 'PyQt4.QtGui.QCommonStyle'>
Fusion QFusionStyle <class 'PyQt4.QtGui.QCommonStyle'>
据我们观察,所有这些都是这种类型。
How do I actually get the style being used by Qt?
前面已经回答了:
print(QtGui.QApplication.style().metaObject().className())
就我而言:
Adwaita::Style
我正在尝试使启用了三态的 QtCheckBox 在其状态为部分状态时在复选框中绘制一个 X。我目前让 Qt 处理样式,使其与操作系统匹配,我想保持这种方式,只更改复选框的绘制方式。
我一直在尝试通过覆盖 Qt 使用的 QStyle 来做到这一点。我测试了 Qt 默认使用的样式:
>>> app = QtGui.QApplication()
>>> type(app.style())
PyQt4.QtGui.QCommonStyle
但是当我尝试 运行 app.setStyle(QtGui.QCommonStyle())
时,GUI 发生了变化,最明显的是复选标记从 QCheckBox
es 中消失了。
我查阅了 QCommonStyle
,根据文档,它只绘制了所有样式共有的元素。
所以我有两个问题:
- 为什么
type(app.style())
返回QCommonStyle
,而看起来使用的样式实际上不是QCommonStyle
? - 我如何实际获得 Qt 使用的样式?
我正在使用 PyQt4。
Why is type(app.style()) returning QCommonStyle, when it appears that the style used is actually not QCommonStyle?
像样式一样使用的类一般继承QCommonStyle,例如在我的例子中我有以下样式:
for key in QtGui.QStyleFactory.keys():
st = QtGui.QStyleFactory.create(key)
print(key, st.metaObject().className(), type(app.style()))
输出:
Adwaita-Dark Adwaita::Style <class 'PyQt4.QtGui.QCommonStyle'>
Adwaita Adwaita::Style <class 'PyQt4.QtGui.QCommonStyle'>
Breeze Breeze::Style <class 'PyQt4.QtGui.QCommonStyle'>
bb10dark QBB10DarkStyle <class 'PyQt4.QtGui.QCommonStyle'>
bb10bright QBB10BrightStyle <class 'PyQt4.QtGui.QCommonStyle'>
cleanlooks QCleanlooksStyle <class 'PyQt4.QtGui.QCommonStyle'>
gtk2 QGtkStyle <class 'PyQt4.QtGui.QCommonStyle'>
cde QCDEStyle <class 'PyQt4.QtGui.QCommonStyle'>
motif QMotifStyle <class 'PyQt4.QtGui.QCommonStyle'>
plastique QPlastiqueStyle <class 'PyQt4.QtGui.QCommonStyle'>
qt5ct-style Qt5CTProxyStyle <class 'PyQt4.QtGui.QCommonStyle'>
QtCurve QtCurve::Style <class 'PyQt4.QtGui.QCommonStyle'>
Windows QWindowsStyle <class 'PyQt4.QtGui.QCommonStyle'>
Fusion QFusionStyle <class 'PyQt4.QtGui.QCommonStyle'>
据我们观察,所有这些都是这种类型。
How do I actually get the style being used by Qt?
前面已经回答了:
print(QtGui.QApplication.style().metaObject().className())
就我而言:
Adwaita::Style