Consider the following code example:
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_()
He has:
- A
QMainWindow, QWidgetas the center widget (red), QVBoxLayoutas a child of the widget. Inside:- A
QTextEdit(as a filler) - A
QWidget(black) containing a QHBoxLayout. Inside:
It looks like this:

I would expect the spaces between the labels to be black because it QHBoxLayoutis a child BlackBar, but it seems that it is BlackBarjust “invisible” between them, and the central widget is “shining”, Why is this?
source
share