我一直在使用 PyQt4 实现一个应用程序。
在这个应用程序中,我想根据用户的选择设置样式,并且我想在不重新启动对话框的情况下设置样式。
这是我的一段影响样式区域的代码:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui
styles = ["Plastique","Cleanlooks","CDE","Motif","GTK+"]
class AppWidget(QWidget):
def __init__(self,parent=None):
super(AppWidget,self).__init__(parent)
global styles # declaring global
# I've skipped the useless codes
horizontalLayout = QHBoxLayout()
self.styleLabel =QLabel("Set Style:")
self.styleComboBox = QComboBox()
self.styleComboBox.addItems(styles) # adding the styles list
horizontalLayout.addWidget(self.styleLabel)
horizontalLayout.addWidget(self.styleComboBox)
# skip more code
self.setLayout(layout)
def getStyle(self):
return self.styleComboBox.currentIndex() # get the current index from combobox
# another way i also implement is :
# return self.styleComboBox.currentText()
# after that i remove the global and directly access using this method
# which is of no success
if __name__ == "__main__":
global styles # declaring global
app = QApplication(sys.argv)
widgetApp = AppWidget()
i = widgetApp.getStyle() # assign the index here
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(styles[i])) # setting the style
widgetApp.show()
app.exec_()
print i
但我一直只得到“Plastique”风格。