简体   繁体   中英

Modifying System Style in PyQt4

I'm trying to make QtCheckBox with tristate enabled draw an X in the checkbox when it's state is partial. I'm currently letting Qt handle the style so it matches the operating system, and I'd like to keep it that way and only change the way the checkbox is drawn.

I've been trying to do this by overriding the QStyle used by Qt. I tested the style that Qt was using by default:

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

But when I try running app.setStyle(QtGui.QCommonStyle()) , the GUI changes, most notably the check marks disappear from the QCheckBox es.

I looked up QCommonStyle , and based on the documentation it draws only the elements that are common to all the styles.

So I have two questions:

  1. Why is type(app.style()) returning QCommonStyle , when it appears that the style used is actually not QCommonStyle ?
  2. How do I actually get the style being used by Qt?

I'm using PyQt4.

Why is type(app.style()) returning QCommonStyle, when it appears that the style used is actually not QCommonStyle?

The classes that are used like style inherit generally of QCommonStyle, for example in my case I have the following styles:

for key in QtGui.QStyleFactory.keys():
    st = QtGui.QStyleFactory.create(key)
    print(key, st.metaObject().className(), type(app.style()))

Output:

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'>

And as we observe all are of that type.

How do I actually get the style being used by Qt?

It is already answered in the previous part:

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

In my case:

Adwaita::Style

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM