The background color of the QWidget subclass cannot be set

The background color of the QWidget subclass cannot be set

As the title says. Solved. The solution is at the bottom of the article.

The background color of the Widget created from the QWidget subclass could not be set. However, when I tried the same with the inheritance source QWidget (), it worked for some reason.

What is this!

I looked it up, so I'll summarize it.

Reproduction code

# coding: utf-8

from PySide2 import QtWidgets, QtGui, QtCore


class Widget(QtWidgets.QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        self.setStyleSheet("background-color:red")

        layout = QtWidgets.QVBoxLayout()

        button_widget = QtWidgets.QPushButton()
        button_widget.setText("Inherited widget")

        layout.addWidget(button_widget)

        self.setLayout(layout)


def generate_widget():
    parent_widget = QtWidgets.QWidget()
    parent_widget.setStyleSheet("background-color:red;")

    layout = QtWidgets.QVBoxLayout()

    button_widget = QtWidgets.QPushButton()
    button_widget.setText("QWidget")

    layout.addWidget(button_widget)

    parent_widget.setLayout(layout)

    return parent_widget


class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        hbox = QtWidgets.QHBoxLayout()

        widget = Widget()
        widget2 = generate_widget()

        hbox.addWidget(widget)
        hbox.addWidget(widget2)

        self.setLayout(hbox)


def main():
    app = QtWidgets.QApplication()

    window = Window()
    window.show()

    exit(app.exec_())


if __name__ == "__main__":
    main()

qWidget_bgcolor_test_01.png

Like this, the background color works only on the background of the button.

Nande?

The Qt Style Sheets Reference says: (Super translation)

QWidget

Only background, background-clip, and background-origin are supported.

** When subclassing from QWidget, you need to provide paintEvent as below. ** **

def paintEvent(self, event): opt = QtWidgets.QStyleOption() opt.init(self) painter = QtGui.QPainter(self) style = self.style() style.drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, painter, self)


 So, if you put the above code in a subclass, it will be solved.
 You did it!

 ![qWidget_bgcolor_test_02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294868/05de106f-8322-b4d3-c8aa-c4794ce7a75c.png)


Recommended Posts

The background color of the QWidget subclass cannot be set
Django cannot be installed in the development environment of pipenv + pyenv
Change the background of Ubuntu (GNOME)
Color extraction with Python + OpenCV solved the mystery of the green background
The story of using mysqlclient because PyMySQL cannot be used with Django 2.2
[pyqtgraph] Set the size ratio of the graph
Set the xticklabels color individually with matplotlib
Set the process name of the Python program
Set the color on the poster side so that the color of the Youtube subtitles changes automatically.
Set the range of active strips to the preview range
Summary of examples that cannot be pyTorch backward
Change the color of Fabric errors and warnings
[Django] css in the project cannot be read
ROS Lecture 119 Correct the color of the camera image
The problem that the ifconfig command cannot be used
Chrome cannot be opened with selenium because the version of Chrome driver does not match
[Python] Change the text color and background color of a specific keyword in print output