Does anybody know, why the Rect drawn in the paintEvent does not cover the exact area of my QWidget but instead starts at an weird offset?

Qt Code:
  1. class ContainerWidget(QWidget):
  2. def __init__(self):
  3. super().__init__()
  4. pal = QPalette()
  5. pal.setColor(QPalette.Window, Qt.magenta)
  6. self.setAutoFillBackground(True)
  7. self.setPalette(pal)
  8. def paintEvent(self, event):
  9. opt = QStyleOption()
  10. opt.initFrom(self)
  11. painter = QPainter(self)
  12. painter.setBrush( Qt.red )
  13. painter.drawRect(self.geometry().x(), self.geometry().y(), self.geometry().width(), self.geometry().height())
  14.  
  15.  
  16. class TopWidget(QWidget):
  17. def __init__(self):
  18. super().__init__()
  19. pal = QPalette()
  20. pal.setColor(QPalette.Window, Qt.yellow)
  21. self.setAutoFillBackground(True)
  22. self.setPalette(pal)
  23. def paintEvent(self, event):
  24. painter = QPainter(self)
  25. painter.setBrush( Qt.green )
  26. painter.drawRect(self.x(), self.y(), self.width(), self.height())
  27.  
  28. class BottomWidget(QWidget):
  29. def __init__(self):
  30. super().__init__()
  31. pal = QPalette()
  32. pal.setColor(QPalette.Window, Qt.gray)
  33. self.setAutoFillBackground(True)
  34. self.setPalette(pal)
  35. def paintEvent(self, event):
  36. painter = QPainter(self)
  37. painter.setBrush( Qt.blue )
  38. painter.drawRect(self.x(), self.y(), self.width(), self.height())
  39.  
  40. def main():
  41. app = QApplication()
  42. containerWidget = ContainerWidget()
  43. containerLayout = QVBoxLayout(containerWidget)
  44. containerWidget.resize(800,600)
  45. containerLayout.setSpacing(0)
  46. containerLayout.setContentsMargins(0,0,0,0)
  47.  
  48.  
  49. containerWidget.show()
  50. sys.exit(app.exec_())
  51.  
  52. if __name__ == "__main__":
  53. main()
To copy to clipboard, switch view to plain text mode 

paintevent_off.jpg