Drawing on top of an image in Qt

There is a program that opens the image:

pixmap = QPixmap("pic.jpg")
self.image = QLabel (self)
self.image.setPixmap(pixmap)
self.image.move(200,10)
self.image.setObjectName("image")

Then, by clicking the mouse on this image, it gets the coordinates of the click point. How do I make the program put a point in these coordinates? I tried to do it through the function:

pen = QPen(Qt.black,10)
qp=QPainter(self.image)
qp.setPen(pen)
qp.drawPoint(x,y)

Nothing comes, it doesn't give out errors, it doesn't draw points. The picture and the drawing function are in the same class. Using python3.4, qt4.

class MainGui(QWidget):

    def __init__(self):
        super().__init__()
        self.init_UI()

    def init_UI(self):
        self.pixmap = QPixmap("pic.jpg")
        self.image = QLabel (self)
        self.image.setPixmap(self.pixmap)
        self.image.move(200,10)
        self.image.setObjectName("image")
        self.image.mousePressEvent = self.get_pos

   def drawPoints(self, pos):
        pen = QPen(Qt.black,10)
        qp=QPainter(self.image)
        qp.setPen(pen)
        qp.begin (self.image)
        qp.drawPoint(pos.x(),pos.y())
        self.image.update()

   def get_pos(self, event):
        X=event.pos().x()
        y=event.pos().y()
        self.drawPoints(event.pos())

if __name__ =='__main__':
     app = QApplication(sys.argv)
     ex = MainGui()
     ex.show()
     sys.exit(app.exec_())
Author: TheSaGe, 2016-10-09

1 answers

Another important point is what properties your point should have.

If you just display and forget, then you can draw directly on QPixmap, if something is more complex, then you need to look at QGraphicsView and QGraphicsItem.

For example like this:

from PyQt4 import Qt


class DrawableLabel(Qt.QLabel):

    def __init__(self, *args, **kwargs):
        super(DrawableLabel, self).__init__(*args, **kwargs)

    def drawPoint(self, pos):
        qp = Qt.QPainter()
        qp.begin(self.pixmap())
        qp.setPen(Qt.Qt.red)
        qp.drawPoint(pos.x(), pos.y())

    def mousePressEvent(self, *args, **kwargs):
        event = args[0]
        self.drawPoint(event.pos())
        self.update()
        return Qt.QLabel.mousePressEvent(self, *args, **kwargs)


if __name__ == '__main__':

    app = Qt.QApplication([])
    label = DrawableLabel()
    pix = Qt.QPixmap("pix.jpg")
    label.setPixmap(pix)
    label.show()

    app.exec()

Your code doesn't work because you have a typo in the name of the MousePressEvent method, but it should be mousePressEvent. That is, your drawPoints method is never called at all. And you need to draw either on the pixmap object QLabel, or override the paintEvent.

Here is the corrected code:

from PyQt4 import Qt
import sys

class MainGui(Qt.QWidget):

    def __init__(self):
        super().__init__()
        self.init_UI()

    def init_UI(self):
        self.pixmap = Qt.QPixmap("pic.jpg")
        self.image = Qt.QLabel (self)
        self.image.setPixmap(self.pixmap)
        self.image.move(200, 10)
        self.image.setObjectName("image")
        self.image.mousePressEvent = self.get_pos

    def drawPoints(self, pos):
        print("Call draw points")
        pen = Qt.QPen(Qt.Qt.black, 10)
        qp = Qt.QPainter()
        qp.begin(self.image.pixmap())
        qp.setPen(pen)
        qp.drawPoint(pos.x(), pos.y())
        self.image.update()

    def get_pos(self, event):
        X = event.pos().x()
        y = event.pos().y()
        self.drawPoints(event.pos())

if __name__ == '__main__':
    app = Qt.QApplication(sys.argv)
    ex = MainGui()
    ex.show()
    sys.exit(app.exec_())
 1
Author: Avernial, 2016-10-13 01:20:19