2017-09-05 80 views
0

我试图让QtCheckBox在启用tristate的状态为部分时在复选框中绘制一个X.我现在让Qt处理风格,使其与操作系统相匹配,我想保持这种状态,只改变复选框的绘制方式。在PyQt4中修改系统风格

我一直在试图通过覆盖Qt使用的QStyle来做到这一点。我测试了Qt的使用默认的样式:

>>> app = QtGui.QApplication() 
>>> type(app.style()) 
PyQt4.QtGui.QCommonStyle 

但是,当我尝试运行app.setStyle(QtGui.QCommonStyle()),图形用户界面的变化,最明显的是查马克从QCheckBox ES消失。

我抬头看QCommonStyle,并根据文档只绘制了所有样式通用的元素。

所以我有两个问题:

  1. 为什么type(app.style())返回QCommonStyle,当它出现,所使用的风格其实并不是QCommonStyle
  2. 我该如何获得Qt使用的风格?

我正在使用PyQt4。

回答

2

为什么类型(app.style())返回QCommonStyle,当它出现的是 使用的样式其实并不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'> 

当我们遵守所有属于那种类型。

我该如何获得Qt使用的风格?

它在前面部分已经回答:

print(QtGui.QApplication.style().metaObject().className()) 

在我的情况:

Adwaita::Style