pyQt4: QWidget subclass not responding to new setStyleSheet() background colour

Posted by Ronny on Stack Overflow See other posts from Stack Overflow or by Ronny
Published on 2010-04-02T09:40:26Z Indexed on 2010/04/02 9:43 UTC
Read the original article Hit count: 559

Filed under:
|
|
|
|

I am having an issue with PyQt4. I want to create a new widget within a window, and I want this widget to have a custom color.

When i create a subclass of the QWidget class, and instantiate it, I am not able to change its background color through the setStyleSheet() function.

When I instantiate a new QWidget object, I have no problems in changing its background color. But i dont want an ordinary QWidget object.I want to create my own subclass of QWidget.

When i create a subclass of a QPushButton, I am also able to change its background color using the setStyleSheet() function.

There are no error messages or warnings in the console window, it just refuses to work properly without any indication as to why.

So what i would like to know is why is it that i can change the background color of a widget if i simply create a QWidget object, or a subclass of QPushButton, but not when i create a subclass of QWidget. And how can i therefore change the background color of an object that is a subclass of QWidget?

Is it maybe something specific to the version of python or PyQt that i am using? Is it a bug in the library? or some flaw in the way that i am writing my code?

I am using python 2.6.4 and PyQt4

Below is an example of the code that leads me to trouble. There are three widgets within the window one below the other. The parent widget is set with background color of green. The top widget is set to red, the middle one, is the subclass of QWidget, which should be blue, but it appears invisible because it takes on the color of the parent window for some reason. and the bottom widget is a subclass of QPushButton and is white.

import sys
from PyQt4 import QtGui, QtCore


################################################################################
#--------------------------------------------------------- CUSTOM WIDGET CLASS 1
class CustomWidget(QtGui.QWidget):
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)
        # some custom properties and functions will follow


################################################################################
#--------------------------------------------------------- CUSTOM WIDGET CLASS 2
class CustomWidget2(QtGui.QPushButton):
    def __init__(self, parent):
        QtGui.QPushButton.__init__(self, parent)
        # some custom properties and functions will follow


################################################################################
#----------------------------------------------------------- PARENT WIDGET CLASS
class Parent(QtGui.QWidget):
    def __init__(self, parent=None):
        #---------------------------------------------------------- SETUP WINDOW
        QtGui.QWidget.__init__(self, parent)
        self.resize(500, 340)
        self.setStyleSheet("QWidget {background-color: #00FF00}")

        #-------------------------------------------------- SETUP DEFAULT WIDGET
        wid1 = QtGui.QWidget(self)
        wid1.setGeometry(10, 10, 480, 100)
        wid1.setStyleSheet("QWidget {background-color: #FF0000 }")

        #------------------------------------------------- SETUP CUSTOM WIDGET 1
        wid2 = CustomWidget(self)
        wid2.setGeometry(10, 120, 480, 100)
        wid2.setStyleSheet("QWidget {background-color: #0000FF }")
        for i in dir(wid2):
            print i

        #------------------------------------------------- SETUP CUSTOM WIDGET 2
        wid3 = CustomWidget2(self)
        wid3.setGeometry(10, 230, 480, 100)
        wid3.setStyleSheet("QWidget {background-color: #FFFFFF }")


################################################################################
#-------------------------------------------------------------------------- MAIN
app = QtGui.QApplication(sys.argv)
win = Parent()
win.show()
app.exec_()

© Stack Overflow or respective owner

Related posts about pyqt4

Related posts about python