eric
eric

Reputation: 8019

Applying setStyle to QLabel in PySide/PyQt

In PySide, I am trying to change the overall style of a simple GUI window (a QLabel object), as outlined at the PySide documentation:

http://srinikom.github.io/pyside-docs/PySide/QtGui/QStyle.html#detailed-description

That is, as described at that link, I want to use setStyle to change the overall GUI format to one of the pre-defined GUI styles (e.g., from Windows to Cleanlooks). (As I note below, this goal is different from tweaking colors and such using setStyleSheet, which I already know how to do).

To get started, I first checked what style types I have available:

print QtGui.QStyleFactory.keys()

This revealed many available styles, including 'Cleanlooks'.

Second, based on documentation from three sources (PySide, ZetCode, and Nullege), I tried to change my QLabel style to 'Cleanlooks':

QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))

Unfortunately, this changed nothing. In fact, nothing I enter has changed a single pixel in my Hello World widget. Not sure if relevant, but when I print the output of the previous line of code, I always get None. Full example code is below the fold.

I am in Windows 7, Python 2.7 running within iPython. Based on replies, this seems to be an issue with PyQt4 in Windows 7 too, not just PySide.

Just to be extra clear, I am not asking about how to use setStyleSheets to tweak my widget. This has been addressed decently already: Styling with classes in Pyside + Python


Example of code I tried (other permutations I tried are shown in comments):

# coding: utf-8

import sys
from PySide import QtGui

class HelloWorldApp(QtGui.QLabel):
    def __init__(self):
        QtGui.QLabel.__init__(self, "Hello, world!") 
        self.initUI()

    def initUI(self): 
        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
        self.show()

def main():
    #QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))  #crashes program
    qt_app=QtGui.QApplication(sys.argv)
    #QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))  #does nothing
    exHell=HelloWorldApp()
    sys.exit(qt_app.exec_())

if __name__=="__main__":
    main()

Upvotes: 0

Views: 7997

Answers (1)

eric
eric

Reputation: 8019

The most helpful resource I found, which answered my question, was this wonderful gallery of widgets:

Widget Gallery

It shows the look of pretty much every type of widget under the different styles.

It turns out my "problem" with QLabel was not a bug, but rather that a bare QLabel is so simple that changing its style rarely influences how it looks. That is, when it is the only thing you have in your display, at best it will not be obvious when you change the style (as in my Hello World example in my question).

As expected, when I replaced the simple QLabel in my example with a combobox or other more complex widget, the style differences showed very clearly, just as described in the Widget Gallery.

Note the above Widget Gallery was pointed out when I asked about this at Qt Centre

Upvotes: 1

Related Questions