Consider the following example code:
from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QLabel, QWidget,
QMainWindow, QVBoxLayout, QTextEdit)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
cwidget = QWidget(self)
cwidget.setStyleSheet("QWidget { background-color: red; }")
self.setCentralWidget(cwidget)
self.resize(100, 100)
vbox = QVBoxLayout(cwidget)
vbox.addWidget(QTextEdit(self))
vbox.addWidget(BlackBar(self))
class BlackBar(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setStyleSheet("* { background-color: black; color: white; }")
hbox = QHBoxLayout(self)
hbox.setSpacing(5)
hbox.addWidget(QLabel(text="eggs"))
hbox.addWidget(QLabel(text="bacon"))
if __name__ == '__main__':
app = QApplication([])
main = MainWindow()
main.show()
app.exec_()
It has:
- A
QMainWindow
,QWidget
as central widget (red),QVBoxLayout
as a child of the cental widget. Inside there:- A
QTextEdit
(just as a filler) - A
QWidget
(black), which contains aQHBoxLayout
. Inside that:- Two
QLabels
- Two
- A
This looks like this:
I'd expect the spaces between the labels to be black, because the QHBoxLayout
is a child of BlackBar
, but it seems BlackBar
is just "invisible" in between and the central widget "shines through". Why is this?