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

3

8

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 }")


        #------------------------------------------------- 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_()
Discrepancy answered 2/4, 2010 at 9:40 Comment(0)
D
6

Well, I discovered a solution, I dont know if its the best one or not, so if anyone else has any suggestions, please leave a comment.

By calling the show() and setAutoFillBackground(True) methods for the QWidget subclass object, i can get the colors to show up. eg:

    wid2.setStyleSheet("QWidget {background-color: #0000FF }")
    wid2.show()
    wid2.setAutoFillBackground(True)
Discrepancy answered 2/4, 2010 at 10:33 Comment(0)
B
2

According to this when you subclass QWidget you have to implement the paintEvent handler.

class CustomWidget(QtGui.QWidget):
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)
        # some custom properties and functions will follow
    def paintEvent(self, event):
        opt = QStyleOption()
        opt.init(self)
        painter = QPainter(self)
        self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)
Beside answered 15/12, 2015 at 22:29 Comment(1)
Init for opt should be opt.initFrom(self) instead.Leptorrhine
G
0

Not in a place to test this right now, but if i remember correctly, i had issues when naming the widget class, and also when using {} grouping when im only adding one stylesheet attribute.

Try running your code, but instead of what you have, use:

self.setStyleSheet("background-color: #00FF00")

Or if its multiple attributes, use:

self.setStyleSheet("background-color: #00FF00; color: #FFFFFF")
Genesa answered 26/9, 2014 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.