Results 1 to 3 of 3

Thread: How to get mouse click position of QLabel which is a child of QMainWindow in PyQt5

  1. #1
    Join Date
    Jun 2018
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Exclamation How to get mouse click position of QLabel which is a child of QMainWindow in PyQt5

    Hi, I am new to PyQt. I am designing an paint interface using PyQt5. For that, I made a class gui(QMainWindow). Then I added a QLabel through qtDesigner and named it imageLabel. So far so good. But now I need the position of mouse click relative to the QLabel (imageLabel).

    I know that in order to get the mouse click position from the QMainWindow i need to call this:
    def mousePressEvent(self, event):
    if event.button() == Qt.LeftButton:
    self.lastPoint= event.pos()

    This gives me the position that is indexed from the top of the QMainWindow, I don't need that. I need mouse position starting relative to my QLabel. This is needed because, I am creating a dynamic label, so upon resizing the window, the position of QLabel will change (hence I can't hard code anything)

    Screenshot (28).jpg See, the white window is where I would like to paint.

    How do I get the position of mouse from this QLabel/QImage.
    Thanks.

  2. #2
    Join Date
    Jul 2008
    Posts
    397
    Thanks
    9
    Thanked 51 Times in 49 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get mouse click position of QLabel which is a child of QMainWindow in PyQt

    Hi, have a lot at the different coordinate mapping functions like QWidget::mapFromGlobal(), QWidget::mapFromParent(), etc.

    Ginsengelf

  3. The following user says thank you to Ginsengelf for this useful post:

    AshishGupta (19th June 2018)

  4. #3
    Join Date
    Jun 2018
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get mouse click position of QLabel which is a child of QMainWindow in PyQt

    Thanks @Ginsengelf. Yes, the function you suggested worked. Thanks a ton.

    Here is the code for others to refer:

    def mousePressEvent(self, event):
    if event.button() == Qt.LeftButton & self.globalDrawing==True:
    self.drawing=True
    self.lastPoint= event.pos()
    self.lastPoint=self.imageLabel.mapFromParent(event .pos()) #this is working fine now
    self.imageLabel.setPixmap(QPixmap.fromImage(self.i mage))

    def mouseMoveEvent(self,event):
    if (event.buttons() & Qt.LeftButton) & self.drawing &self.globalDrawing:
    painter=QPainter(self.image)
    painter.setPen(QPen(self.brushColor,self.brushSize ,Qt.SolidLine,Qt.RoundCap,Qt.RoundJoin))
    painter.drawLine(self.imageLabel.mapFromParent(eve nt.pos()),self.lastPoint)
    self.lastPoint=self.imageLabel.mapFromParent(event .pos()) #this is working fine now
    self.imageLabel.setPixmap(QPixmap.fromImage(self.i mage))
    def mouseReleaseEvent(self,event):
    if event.button == Qt.LeftButton & self.globalDrawing:
    self.drawing = False
    self.imageLabel.setPixmap(QPixmap.fromImage(self.i mage))

Similar Threads

  1. Replies: 0
    Last Post: 19th August 2015, 08:44
  2. Get mouse position on click?
    By N3wb in forum Qt Programming
    Replies: 2
    Last Post: 21st August 2010, 23:33
  3. how to get the position of mouse click on a label
    By qt_user in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2010, 09:14
  4. Word at mouse click position in QGraphicsTextItem
    By pherthyl in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2008, 04:56
  5. Clearing a QLabel since the mouse position
    By SkripT in forum Qt Programming
    Replies: 10
    Last Post: 18th January 2006, 18:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.