我想要一个切换按钮,根据切换状态显示不同的图像。该功能在默认QApplication样式(“WindowsVista”)中工作正常。但是WindowsVista的风格很难看,我宁愿使用Fusion风格。不幸的是,图像的融合样式没有改变
守则:
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton,
QStyleFactory, QLabel)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QSize
from PyQt5 import Qt, QtCore
import sys
class Test(QMainWindow):
def __init__(self, parent=None):
super(Test, self).__init__(parent=parent)
self.setGeometry(50,50,300,400)
icon = QIcon()
icon.addFile('red.png', state = QIcon.Off)
icon.addFile('green.png', state = QIcon.On)
button = QPushButton(parent = self)
button.setCheckable(True)
button.setIcon(icon)
button.setIconSize(QSize(150,150))
button.setGeometry(50,50,200,200)
verString = 'PyQt version: ' + Qt.PYQT_VERSION_STR + '\n'
verString += 'Qt version: ' + QtCore.qVersion()
print(verString)
label = QLabel(verString, parent = self)
label.setGeometry(50, 250, 200, 150)
if __name__ == '__main__':
app = QApplication(sys.argv)
#app.setStyle(QStyleFactory.create('fusion'))
app.setStyle(QStyleFactory.create('windowsvista'))
test = Test()
test.show()
sys.exit(app.exec_() )
WindowsVista样式(左上按钮,右下按钮):
融合风格(左上按钮,右下按钮):
这似乎是一种默认行为,因为在source code中可以看到融合样式:
一种可能的解决方案是实现覆盖此行为的QProxyStyle:
相关问题 更多 >
编程相关推荐