Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting style in using QStyleFactory from a list of styles in a QComboBox

I've been implementing an application using PyQt4.

screenshot2

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.

like image 564
dragfire Avatar asked Jan 06 '14 21:01

dragfire


1 Answers

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:

import sys
from PyQt4 import QtCore, QtGui

class AppWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(AppWidget, self).__init__(parent)
        horizontalLayout = QtGui.QHBoxLayout()
        self.styleLabel = QtGui.QLabel("Set Style:")
        self.styleComboBox = QtGui.QComboBox()
        # add styles from QStyleFactory
        self.styleComboBox.addItems(QtGui.QStyleFactory.keys())
        # find current style
        index = self.styleComboBox.findText(
                    QtGui.qApp.style().objectName(),
                    QtCore.Qt.MatchFixedString)
        # set current style
        self.styleComboBox.setCurrentIndex(index)
        # set style change handler
        self.styleComboBox.activated[str].connect(self.handleStyleChanged)
        horizontalLayout.addWidget(self.styleLabel)
        horizontalLayout.addWidget(self.styleComboBox)
        self.setLayout(horizontalLayout)

    # handler for changing style
    def handleStyleChanged(self, style):
        QtGui.qApp.setStyle(style)

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    widgetApp = AppWidget()
    widgetApp.show()
    sys.exit(app.exec_())
like image 184
ekhumoro Avatar answered Sep 25 '22 05:09

ekhumoro