I'm trying to draw a shape over a widget and from what I understand you can do this by overriding the paintEvent()
method. However, when I do so, the shape is rendered at the back. How can I render the shape in front?
I'm using pyqt5 and my test code is:
from PyQt5 import Qt
from PyQt5.QtGui import QPainter, QPen,QBrush,QColor
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout,QPushButton
import sys
class MyWidget(QWidget):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.btn = QPushButton('Hello World')
self.layout = QHBoxLayout()
self.layout.addWidget(self.btn)
self.setLayout(self.layout)
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.green))
brush = QBrush(QColor(0,0,255,255))
painter.setBrush(brush)
painter.drawRect(0,0,50,50)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainscreen = MyWidget()
mainscreen.show()
app.exec_()