When I used a custom Widget as the CentralWidget for QMainWindow in PyQt6, there was a gap between the CentralWidget and the QMainWindow? I wonder why that is.Here's my code:
from PyQt6.QtWidgets import QApplication, QWidget, QMainWindow, QLabel, QHBoxLayout
import sys
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self._layout = QHBoxLayout(self)
self.label = QLabel("hello")
self.label.setStyleSheet("QLabel {color: white;}")
self._layout.addWidget(self.label)
self.setStyleSheet("background-color: black; color: white;")
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# When using custom widgets, there is a gap
self.central_widget = MyWidget()
# When using Qwidget, there is no gap
# self.central_widget = QWidget()
# self.central_layout = QHBoxLayout()
# self.central_layout.addWidget(QLabel("hello"))
# self.central_widget.setLayout(self.central_layout)
self.setCentralWidget(self.central_widget)
# self.central_widget.setStyleSheet("background-color: black; color: white; border: none;")
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec())
I want to know why there is a difference between using a custom widget and using a QWidget, and how to fix it.