PyQt4: subclass of QWidget does not respond to new background color setStyleSheet ()

I have a problem with PyQt4. I want to create a new widget in a window, and I want this widget to have its own color.

When I create a subclass of the QWidget class and instantiate it, I cannot change its background color using the setStyleSheet () function.

When I instantiate a new QWidget, I have no problem changing its background color. But I do not want a regular QWidget object. I want to create my own subclass of QWidget.

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

There are no error messages or warnings in the console window; it simply refuses to work properly without indicating why.

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

Perhaps this is something specific to the python or pyQt version I'm using? Is this a bug in the library? or some flaw in the way i write my code?

I am using python 2.6.4 and PyQt4

Below is an example of code that leads me to trouble. Inside the window there are three widgets one by one. The parent widget is set with the background color of green. The top widget is set to red, the middle one is a subclass of QWidget, which should be blue, but it seems invisible, because for some reason it takes the color of the parent window. and the bottom widget is a subclass of QPushButton and 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_() 
+7
python stylesheet pyqt pyqt4 qwidget
source share
3 answers

Well, I found a solution, I do not know if it is the best or not, so if anyone has any suggestions, leave a comment.

By calling the show () and setAutoFillBackground (True) methods on a QWidget subclass object, I can get the colors to display. eg:

  wid2.setStyleSheet("QWidget {background-color: #0000FF }") wid2.show() wid2.setAutoFillBackground(True) 
+5
source share

According to this , when you subclass QWidget, you need to implement a 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) 
+2
source share

There is no place to check this right now, but if I remember correctly, I had problems assigning a widget class name, as well as using {} grouping, when im only adds one attribute to the stylesheet.

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

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

Or, if its several attributes, use:

 self.setStyleSheet("background-color: #00FF00; color: #FFFFFF") 
0
source share

All Articles