To this question I am referring to the answer from @Kuba Ober Draw Rectangular overlay on QWidget at click
My problem: I dont know how to "translate" the C++ (or C?) into Python. :-(
Thus I ask this "duplicated" question again, and wish anyone would help to rewrite the code to achieve the overlay effect in PyQt5.
As an example, I prepare here some code:
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MyApp(QWidget):
def __init__(self):
super(MyApp, self).__init__()
self.initUI()
def initUI(self):
self.text = "hello world"
self.setGeometry(100, 100, 400, 300)
self.setWindowTitle('Draw Demo')
self.btn = QPushButton("Butten should be overlayed", self)
self.btn.setFixedSize(200, 200)
self.btn.move(40, 40)
self.show()
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
qp.setPen(QColor(Qt.red))
qp.setFont(QFont('Arial', 20))
qp.drawText(10, 50, "hello Python")
qp.setPen(QColor(Qt.blue))
qp.drawLine(10, 100, 100, 100)
qp.drawRect(10, 150, 150, 100)
qp.setPen(QColor(Qt.red))
qp.drawEllipse(100, 50, 100, 50)
# qp.drawPixmap(220, 10, QPixmap("python.jpg"))
qp.fillRect(200, 175, 150, 100, QBrush(Qt.SolidPattern))
qp.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
Target is: Let the Button shown in the background (overlayed by QPainter), but it is transparent to mouse event. Thus I could click the button, even though it is covered by QPainter.
Any help will be highly appreciated!