简体   繁体   English

pyQt4:QWidget子类没有响应新的setStyleSheet()背景颜色

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

I am having an issue with PyQt4. 我遇到了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. 当我创建QWidget类的子类并实例化它时,我无法通过setStyleSheet()函数更改其背景颜色。

When I instantiate a new QWidget object, I have no problems in changing its background color. 当我实例化一个新的QWidget对象时,我在更改其背景颜色方面没有任何问题。 But i dont want an ordinary QWidget object.I want to create my own subclass of QWidget. 但我不想要一个普通的QWidget对象。我想创建自己的QWidget子类。

When i create a subclass of a QPushButton, I am also able to change its background color using the setStyleSheet() function. 当我创建QPushButton的子类时,我也可以使用setStyleSheet()函数更改其背景颜色。

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. 所以我想知道的是,为什么我可以改变窗口小部件的背景颜色,如果我只是创建一个QWidget对象,或QPushButton的子类, 但不是当我创建QWidget的子类时。 And how can i therefore change the background color of an object that is a subclass of QWidget? 那么我怎样才能改变作为QWidget子类的对象的背景颜色?

Is it maybe something specific to the version of python or PyQt that i am using? 它可能是我正在使用的python或PyQt版本特有的东西吗? 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 我使用的是python 2.6.4和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. 顶部小部件设置为红色,中间的小部件是QWidget的子类,它应该是蓝色的,但它看起来是不可见的,因为它出于某种原因呈现父窗口的颜色。 and the bottom widget is a subclass of QPushButton and is white. 底部小部件是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_()

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. 通过调用QWidget子类对象的show()setAutoFillBackground(True)方法,我可以显示颜色。 eg: 例如:

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

According to this when you subclass QWidget you have to implement the paintEvent handler. 根据这个,当你继承QWidget时,你必须实现paintEvent处理程序。

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)

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. 现在没有在这个地方进行测试,但是如果我没记错的话,我在命名widget类时遇到了问题,而且当我只添加一个样式表属性时使用{}分组时也是如此。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM