I've been implementing an application using PyQt4.
In this application I want to set the style according to the user's choice, and I want to set the style without restarting the dialog again.
Here's my piece of code which is affecting the styling area:
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
But I keep getting only the "Plastique" style.
You don't need a global list of styles, because that is already available from QStyleFactory.keys.
What you need to do is load those keys into the combo-box, set the combo-box index to the current style, and then connect the combo-box
activated
signal to a handler so that the style can be changed.Something like this should work: