T.C
T.C

Reputation: 31

paint over QLabel with PyQt

For a Python project, I have to draw an image with a painter over a Qlabel widget already filled by a picture. My problem is that my image is systematically drawn behind my Label. I simplified the problem by this code, my goal is to paint "cigale1.png" onto "arrierplan.png" which is in a Label :

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from test_ihm_main import *


class MonAppli(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.Label.setPixmap(QtGui.QPixmap("arrierplan.png"))
        self.paintEvent(self.ui.Label)

    def paintEvent(self,e):
        qp = QtGui.QPainter(self)
        qp.begin(self)
        qp.drawPixmap(450,50,QtGui.QPixmap("cigale1.png"))
        qp.end()  



if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MonAppli()
    window.show()
    app.exec_()

And here is my Ui_MainWindow class :

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.Quitter = QtWidgets.QPushButton(self.centralwidget)
        self.Quitter.setGeometry(QtCore.QRect(670, 520, 113, 32))
        self.Quitter.setObjectName("Quitter")
        self.Label = QtWidgets.QLabel(self.centralwidget)
        self.Label.setGeometry(QtCore.QRect(400, 30, 361, 231))
        self.Label.setText("")
        self.Label.setObjectName("Label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.Quitter.clicked.connect(MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.Quitter.setText(_translate("MainWindow", "Quitter"))

I ve read the doc a lot, read a lot of topic but nothing worked...

Thanks by advance,

T.C

Upvotes: 3

Views: 4895

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

The problem arises because the QLabel is above the QMainWindow, and when you apply the function this will be drawn on it and so it will not be displayed.

The paintEvent function should be overwritten in QLabel, for this we will create a class that inherits from Qlabel called Label.

class Label(QtWidgets.QLabel):
    def __init__(self, parent=None):
        super(Label, self).__init__(parent=parent)

    def paintEvent(self, e):
        super().paintEvent(e)
        qp = QtGui.QPainter(self)
        qp.drawPixmap(100,100,QtGui.QPixmap("cigale1.png")) 

To be able to use it change add it in the file of the class Ui_MainWindow, and you must change:

self.Label = Label(self.centralwidget)

to

self.Label = QtWidgets.Qlabel(self.centralwidget)

Results:

arrierplan.png

enter image description here

cigale1.png

enter image description here

output

enter image description here

Upvotes: 2

Related Questions