0

I'm doing a desktop app where I made multiple classes inherit from QWidget, and the problem is that when I try to apply color to the background of each one, it doesn't seem to be working.

I expect a black background of the main app and a white background for the apps (which contains other widgets but for now I'm just figuring out the background), but instead I get a totally black window.


class Main(QMainWindow):

    def __init__(self):
        super().__init__()        

        self.main_widget = QWidget(self)
        self.setCentralWidget(self.main_widget)
        self.main_widget.setStyleSheet('background-color: black')

        self.LO = QVBoxLayout(self.main_widget)
        self.square1 = Square()
        self.square2 = Square()

        self.LO.addWidget(self.square1)
        self.LO.addWidget(self.square2)


class Square(QWidget):

    def __init__(self):
        super().__init__()
        self.setStyleSheet('background-color: #FFFFFF')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = Main()
    mainWin.show()
    sys.exit(app.exec_())

This is what I expect to happen (background black, widget white):

expected result

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

1 Answers1

0

It seems that the classes inheriting from QWidget didn't adopt the Stylesheet but when you inherit from QFrame, it automatically works as expected. I don't know why in QtDesigner it seems to work with QWidgets as they were frames.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135