pyQt4:QWidget子类不响应新的setStyleSheet()背景颜色
我在使用 PyQt4 时遇到问题。我想在窗口中创建一个新的小部件,并且我希望该小部件具有自定义颜色。
当我创建 QWidget 类的子类并实例化它时,我无法通过 setStyleSheet() 函数更改其背景颜色。
当我实例化一个新的 QWidget 对象时,更改其背景颜色没有问题。但我不想要一个普通的 QWidget 对象。我想创建我自己的 QWidget 子类。
当我创建 QPushButton 的子类时,我还可以使用 setStyleSheet() 函数更改其背景颜色。
控制台窗口中没有错误消息或警告,它只是拒绝正常工作,没有任何说明原因。
所以我想知道的是,为什么如果我只是创建一个 QWidget 对象或 QPushButton 的子类,我可以更改小部件的背景颜色,但当我创建 QPushButton 的子类时却不能更改小部件的背景颜色QWidget。因此,我如何更改 QWidget 子类的对象的背景颜色?
这可能是我正在使用的 python 或 PyQt 版本特有的东西吗?这是库中的错误吗?或者我编写代码的方式存在一些缺陷?
我正在使用 python 2.6.4 和 PyQt4
下面是导致我遇到麻烦的代码示例。窗口中有三个小部件,一个在另一个下面。父窗口小部件设置为绿色背景色。顶部的小部件设置为红色,中间的小部件是 QWidget 的子类,它应该为蓝色,但它看起来不可见,因为由于某种原因它采用了父窗口的颜色。底部小部件是 QPushButton 的子类,并且是白色的。
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_()
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_()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我发现了一个解决方案,我不知道它是否是最好的,所以如果其他人有任何建议,请发表评论。
通过调用 QWidget 子类对象的 show() 和 setAutoFillBackground(True) 方法,我可以显示颜色。例如:
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:
根据this,当您子类化 QWidget 时,您必须实现 PaintEvent 处理程序。
According to this when you subclass QWidget you have to implement the paintEvent handler.
现在还没有地方测试这一点,但如果我没记错的话,我在命名小部件类时遇到了问题,并且在仅添加一个样式表属性时使用 {} 分组时也遇到了问题。
尝试运行您的代码,但不要使用您拥有的代码,而是使用:
或者如果它有多个属性,请使用:
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:
Or if its multiple attributes, use: