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
python stylesheet pyqt pyqt4 qwidget
Ronny
source share