Results 1 to 3 of 3

Thread: PyQt4 QGraphicsView and 'pressed' signal

  1. #1
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Default PyQt4 QGraphicsView and 'pressed' signal

    Hello, i'm having trouble detecting the 'pressed' signal from a QGraphicsView using the signal/slots framework in PyQt4 (v4.5.4, Qt v4.5.2).

    The attached code perhaps best illustrates my problem. The default event handler 'mousePressEvent' fires as expected, but the slot 'slotPressEvent' does not respond to the 'pressed' signal. Can anyone see what the problem is?

    The workaround i'm currently using is commented out on line 13, emitting the 'pressed' signal from the 'mousePressEvent' handler. In my actual application I am also going to trap the 'released' signal on the QGraphicsView, which I am currently doing using the same workaround.

    I expect i'm not understanding something about Qt's signals/events. Coming from experience with Javascript's various event listeners may have confused me as to how they actually work. In JS you can attach a single handler directly as a property of the object button.click, or multiple handlers using: button.addEventListener('click', function). I think of 'mousePressEvent' as being equivalent to button.click, and signals/slots to addEventListener. Is this right?
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PyQt4 QGraphicsView and 'pressed' signal

    In fact, responding with mouse events does not requires the use of SIGNAL/SLOT mechanism.

    I dont know Python but in C++, you need to derived your widget and reimplement the method QWidget::mousePressEvent(QMouseEvent).
    Be aware of QEvent derived argument .

    Then, you are free to emit other signals for your own design.

    In the sample you provided, I notice that :
    * you emit a signal in response of pressEvent but why not direclty call the function you want to be executed
    * the arguments between the signal you emitted and the slot you have connected to it dont match. It cant work. Don't you have any warnings somewhere about failed connect?

  3. #3
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Default Re: PyQt4 QGraphicsView and 'pressed' signal

    Thanks for your reply. In my actual application i'm trying to use signals/slots to combine the functions dealing with remapping the QGraphicsView drag button from left to middle. The following is the relevant code i've lifted from my application:

    Qt Code:
    1. #TODO: Make signals work properly
    2. def mousePressEvent(self, event):
    3. self.emit(SIGNAL("pressed"), (event))
    4. QtGui.QGraphicsView.mousePressEvent(self, event)
    5. def mouseReleaseEvent(self, event):
    6. self.emit(SIGNAL("released"), (event))
    7. QtGui.QGraphicsView.mouseReleaseEvent(self, event)
    8. ######
    9.  
    10. #Assigned via self.connect(self, SIGNAL('pressed'), self.grabCanvas) in __init__
    11. def grabCanvas(self, event):
    12. if event.buttons() == QtCore.Qt.MidButton:
    13. origMode = self.dragMode()
    14. self.setDragMode(self.ScrollHandDrag)
    15. #Send a fake LMB press, TODO: Disable scene's selection of objects
    16. fake_event = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress, event.pos(),
    17. QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.KeyboardModifiers())
    18. QtGui.QGraphicsView.mousePressEvent(self, fake_event)
    19.  
    20. #Add a handler to catch the mouse up event
    21. def revert(event):
    22. self.setDragMode(origMode)
    23. self.disconnect(self, SIGNAL("released"), revert)
    24.  
    25. self.connect(self, SIGNAL("released"), revert)
    To copy to clipboard, switch view to plain text mode 

    As you can perhaps see, i'm trying to make a temporary slot to reset some parameters when the button is released. I felt the most elegant and unobtrusive way of doing this would be using signals/slots rather than two event listeners and a bunch of shared flags (like middleButtonPressed, originalDragMode etc.).
    Last edited by splondike; 8th October 2009 at 13:36. Reason: Clarify how grabCanvas assigned

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.