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") 

Next, by clicking on the image, it receives the coordinates of the point of the click. How to make the program put a point in these coordinates? Tried to do through the function:

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

Nothing comes, no errors, draws no points. Picture and drawing function in the same class. I use 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_()) 
  • I did not write on python, but extrapolating my experience from C ++, I would assume that you need to try qp.begin(self.image); qp.drawPoint(x,y); qp.end(); qp.begin(self.image); qp.drawPoint(x,y); qp.end(); - yrHeTaTeJlb
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • Check the coordinates x and y, and the size of the image - perhaps beyond the borders of the image. In addition, try drawing not on QLabel, but on its pixmap, since I don’t think that the changes will remain on the widget (you’re not drawing in paintEvent). - gil9red
  • The coordinates are exactly correct. In the picture fall. I try through pixmap, also does not come out - TheSaGe

1 answer 1

Another important point is what properties your point should have.

If you simply display and forget, you can draw directly on QPixmap , if something more complicated, 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 does not work because you have a typo in the name of the MousePressEvent method, and there must be a mousePressEvent . Those. your drawPoints method is never called at all. And the drawing needs to be done either on the pixmap of the QLabel object, or by overriding the paintEvent method.

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_()) 
  • I tried to do it differently by your example, the effect is the same. Only when I wrote pixmap () produced an error "object is not callable". Yes, points can be simply displayed and forgotten later, just so that they remain on the screen while the program is running - TheSaGe
  • Tried to run my code? If you get an error for pixmap, then you have redefined it somewhere, since the pixmap method is in the QLabel class, which returns the current QPixmap. And what version of PyQt4 do you have? What operating system do you run under? - Avernial
  • Your code works, but I could not extrapolate it to my case. I wrote a more detailed code in the question itself, please take a look, maybe understand what's wrong. Thank! - TheSaGe
  • Added your corrected code in response. - Avernial
  • "mouse" was in small letters. It was in qp.begin (self.image.pixmap ()) Thank you! Now everything works - TheSaGe