效果:
QStyle是封装了GUI界面组件外观的抽象类,Qt定义了QStyle类的一些子类,应用于不同的操作系统,如QWindowsStyle、QMacStyle等。这些样式是Qt CUI模块自带的,在不停的平台上编译运行的程序具有缺省的样式,QApplication::style()可以返回应用程序缺省的样式
使用Qt的内置样式,可以通过QStyleFactory::keys()获取运行平台支持的样式列表,然后用QStyleFactory::create创建样式,最后用QApplication::setStyle()设置样式
def __init__(self, parent=None):
# 设置自定义样式
super(WidgetGallry, self).__init__(parent)
self.originalPalette = QApplication.palette()
# 下拉列表
style_comboBox = QComboBox()
style_comboBox.addItems(QStyleFactory.keys())
# 标签
style_lable = QLabel('样式')
style_lable.setBuddy(style_comboBox)
# 复选框
self.use_stander_checkbox = QCheckBox('使用标准样式')
self.use_stander_checkbox.setChecked(True)
#下拉列表选择不同的样式,进行更改
style_comboBox.activated[str].connect(self.changeStyle)
self.use_stander_checkbox.toggled.connect(self.changePalette)
# 改变模板
def changePalette(self):
if self.use_stander_checkbox.isChecked():
QApplication.setPalette(QApplication.style().standardPalette())
else:
QApplication.setPalette(self.originalPalette)
# 改变样式
def changeStyle(self, style_name):
QApplication.setStyle(QStyleFactory.create((style_name)))
self.changePalette()