简体   繁体   English

pyside2中的自定义小部件背景颜色

[英]Custom widget background color in pyside2

Trying to work out how to set the background color in a QWidget.试图弄清楚如何在 QWidget 中设置背景颜色。 Here is my code:这是我的代码:

class ParentTester(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        left = ColorTester(self)
        right = QFrame(self)
        right.setFrameStyle(QFrame.Panel | QFrame.Sunken)
        layout = QHBoxLayout()
        layout.addWidget(left)
        layout.addWidget(right)
        self.setLayout(layout)


class ColorTester(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        palette = self.palette()
        palette.setColor(QPalette.Window, QColor(128, 0, 0))
        self.setPalette(palette)

def main():
    import sys
    from PySide2.QtWidgets import QApplication

    app = QApplication([])

    works = True

    if works:
        win = ColorTester()
    else:
        win = ParentTester()
    win.show()
    win.resize(640, 480)

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

This works if I create the class as a topmost window.如果我将 class 创建为最顶层的 window,则此方法有效。 However if I make it a child of another control, the background color goes back to the default.但是,如果我使它成为另一个控件的子控件,则背景颜色将恢复为默认值。 Some of the other color roles do take effect, but not the background color.其他一些颜色角色确实生效,但不是背景颜色。 Not only that, but the colors get passed through to child controls.不仅如此,colors 还可以传递给子控件。

How can I change the background color of a control but not its child controls?如何更改控件的背景颜色而不是其子控件?

By default, the child widgets take the color of the window, so you observe that effect, if you want the custom background color to be used then you must enable the autoFillBackground property:默认情况下,子小部件采用 window 的颜色,因此您可以观察到这种效果,如果要使用自定义背景颜色,则必须启用autoFillBackground属性:

class ColorTester(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        palette = self.palette()
        palette.setColor(QPalette.Window, QColor(128, 0, 0))
        self.setPalette(palette)
        self.setAutoFillBackground(True)

在此处输入图像描述

I finally settled on overriding paintEvent for my widget.我最终决定为我的小部件覆盖paintEvent。 Setting the color in the palette always seems to pass the color down to child controls, which is not what I wanted.在调色板中设置颜色似乎总是将颜色传递给子控件,这不是我想要的。 Here is an example of what worked for me.这是一个对我有用的例子。 This is a QFrame that takes the default background color and darkens and green-shifts it slightly.这是一个 QFrame,它采用默认背景颜色并稍微变暗和变绿。

class GreenFrame(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        r, g, b, a = self.palette().color(QPalette.Window).toTuple()
        self._bgcolor = QColor(r * 7 // 8, g, b * 7 // 8)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.fillRect(event.rect(), self._bgcolor)
        super().paintEvent(event)

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

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