0

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()
Community
  • 1
  • 1
eric
  • 7,142
  • 12
  • 72
  • 138
  • Did you try some other available styles? `plastique` for example. – qurban Jun 09 '14 at 17:54
  • @qurban. Yes: I tried every style that was listed when I printed the keys, which includes--[u'Windows', u'WindowsXP', u'WindowsVista', u'Motif', u'CDE', u'Plastique', u'Cleanlooks']. And I tried them with/without the unicode symbol (u). – eric Jun 09 '14 at 18:46
  • 1
    For what it's worth, I've had the same problem with PySide - but miraculously, it worked with PyQt. – Bo Milanovich Jun 09 '14 at 20:03
  • @Deusdies Ugh. I'm using Spyder, so don't think I can use PyQt. :O I'll go search around the PySide developers site to try to bring attention to this. Hopefully I'm not doing anything silly. – eric Jun 09 '14 at 20:22
  • Yes, you can use it in Spyder. An IDE has nothing to do with what libraries you're using. – Bo Milanovich Jun 10 '14 at 13:42
  • Deusdies ah apparently you are right. I meant anaconda (in which I use spyder), but apparently that problem has been fixed: http://stackoverflow.com/questions/21637922/how-to-install-pyqt4-in-anaconda – eric Jun 10 '14 at 14:32
  • Using `QApplication.setStyle` works for me in Windows 7, Python 2.7 and PySide 1.2.2. Do you have a full minimal example you can provide where you have trouble? – hackyday Jun 11 '14 at 00:20
  • @hackyday I'll add it to the question. – eric Jun 11 '14 at 02:50
  • 2
    `QLabel` is just some text on an plain background, so with this as your only widget won't show (or be very difficult to tell) any style changes. Try using a different widget to test it with, such as `QComboBox`, and you should see a change of style. – hackyday Jun 11 '14 at 05:35
  • hackyday you nailed it. Combobox shows differences. I thought it was supposed to show different style (e.g., rounded corners) to the main window as well, but apparently not! Perhaps you could turn this into an answer so I can accept it? Is there documentation anywhere that describes exactly which widgets that setstyle actually matters for? I guess later tonight after work I could go through all the main widgets and do it by trial and error, but I'd rather see a list someone already did. :) – eric Jun 11 '14 at 12:27

1 Answers1

1

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

eric
  • 7,142
  • 12
  • 72
  • 138