I am trying to subclass and draw on top of a QSpinBox by overriding its paintEvent. For example I try to draw an orange rect covering the entire spin box, but it only ends up going on top of some elements, and remains behind others, and moreover I discovered that passing/returning the paintEvent still draws the widget, so I am not sure I understand what is happening here. This is all on Windows btw.
So my precise question is, how do I draw a rect on top of a QSpinBox, such that it paitns on top of everythign else and is part of the subclassed widget's code (not externally painted)?
My second question is how to override the painting such that when the widget is shown it is not drawn at all?
My code:
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QSpinBox, QWidget
from PyQt6.QtGui import QPaintEvent, QBrush, QColor, QPainter
class CustomSpinBox(QSpinBox):
def paintEvent(self, e: QPaintEvent) -> None:
super().paintEvent(e)
painter = QPainter(self)
painter.setBrush(QBrush(QColor('orange'), Qt.BrushStyle.SolidPattern))
painter.drawRect(0, 0, self.width(), self.height())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
window.setGeometry(500, 200, 400, 300)
spin_box = CustomSpinBox(window)
spin_box.move(100, 100)
spin_box.setFixedSize(120, 40)
window.show()
app.exec()
This paints over the spin box arrows, but below the actual value field. I am not sure how and where this layering order is defined, I assume it is something to do with how SubControls of ComplexControls are handled, but I could not really understand how this works exactly based on the information I found.