Results 1 to 2 of 2

Thread: Associating widgets to mouseevents properly

  1. #1
    Join Date
    Aug 2010
    Location
    Chennai
    Posts
    21
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Associating widgets to mouseevents properly

    I'm using PyQt4 and i want to draw a line based on user's click on an existing image which is displayed as an imagelabel . The image shows properly and after a clicking an icon in toolbar , the user will draw a line on the image .

    I've overridden the mousePressEvent() and mouseReleaseEvent() to get the x,y positions. I've defined paintEvent() to draw the line .

    Qt Code:
    1. def mousePressEvent(self,event):
    2. self.startx=event.x()
    3. self.starty=event.y()
    4. def mouseReleaseEvent(self,event):
    5. self.endx=event.x()
    6. self.endy=event.y()
    7.  
    8. def paintEvent(self,event):
    9. painter=QPainter()
    10. painter.begin(self)
    11. painter.setPen(QPen(Qt.darkGray,3))
    12. painter.drawLine(self.startx,self.starty,self.endx,self.endy)
    13. painter.end()
    To copy to clipboard, switch view to plain text mode 
    PROBLEM :

    i )Since i used 'self' for mouseevents , the error says: object has no attribute 'self.startx' - ( How should i associate a widget to mouseevents in PyQt ? )
    ii )paintEvent() gets called even when i move the mouse around the app.

    thanx in advance..

  2. #2
    Join Date
    Aug 2010
    Location
    Chennai
    Posts
    21
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Associating widgets to mouseevents properly

    I've found a way to avoid unnecessary calling of paintEvent() by defining that in a separate class . It will get called only after releasing the mouse .
    Qt Code:
    1. class line(QtGui.QWidget):
    2. def __init__(self, point1, point2):
    3. self.p1 = point1
    4. self.p2 = point2
    5.  
    6. def paintEvent(self,event):
    7. painter=QPainter()
    8. painter.begin(self)
    9. painter.setPen(QPen(Qt.darkGray,3))
    10. painter.drawLine(self.p1,self.p2)
    11. painter.end()
    12. def mousePressEvent(self,event):
    13. self.startx=event.x()
    14. self.starty=event.y()
    15. def mouseReleaseEvent(self,event):
    16. self.endx=event.x()
    17. self.endy=event.y()
    18. newLine = line(QPoint(self.startx, self.starty),QPoint(self.endx,self.endy))
    To copy to clipboard, switch view to plain text mode 

    But the problem now is :
    paintEvent() doesn't get called .
    The constructor of that object gets called . ( i've added print() statements to find out the flow of control )

    Anyone know why paintEvent doesn't get called ?

Similar Threads

  1. Replies: 3
    Last Post: 28th November 2010, 14:06
  2. qwt mouseEvents
    By halberdier83 in forum Qwt
    Replies: 7
    Last Post: 22nd April 2009, 12:37
  3. QGraphicsView mouseEvents...?
    By thill2708 in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2006, 20:20
  4. Associating icon with a file type
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2006, 16:57
  5. Associating file extensions
    By fullmetalcoder in forum General Programming
    Replies: 9
    Last Post: 10th May 2006, 13:12

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.