Search code examples
pythonpython-2.7eventspyqtsignals-slots

Signaling from a MouseReleaseEvent


I'm not sure what I am doing wrong, basically I want to emit a signal(custom) whenever the mouserelease event happens.

Class myWidget(QWidget):

  def mouseReleaseEvent(self,event):
    if event.button()==Qt.LeftButton:
        message="LEFT BUTTON HAS BEEN CLICKED"
        QtCore.QObject.emit(self,QtCore.SIGNAL('message(QString)') str(message))

When I do this nothing happens I cannot see the signal being emitted, I decided to move

    message="LEFT BUTTON HAS BEEN CLICKED"
    QtCore.QObject.emit(self,QtCore.SIGNAL('message(QString)') str(message))

into the PaintEvent, just so I could determine if anything was being fired. To my surprise within the paintevent, I can see the event being fired (as I am able to consume the message). However when I put it into the mouseReleaseEvent or even the MousePressEvent, nothing happens?

What am i doing wrong. Note I did try putting a print "hello world" line directly into the mouseReleaseEvent, just to test and I can see the print statement being executed as expected. Why am I not seeing the MouseReleaseEvent?

Using pyqt,python and windows. This is very odd


Solution

  • Don't use the old-style signal syntax, as it's very easy to get it wrong and hard to debug. Use the new style syntax instead:

    from PyQt4 import QtCore, QtGui    
    
    class MyWidget(QtGui.QWidget):
        testSignal = QtCore.pyqtSignal(object)
    
        def __init__(self, parent=None)
            super(MyWidget, self).__init__(parent)
            self.testSignal.connect(self.testSlot)
    
        def testSlot(self, message):
            print(message)
    
        def mouseReleaseEvent(self, event):
            self.testSignal.emit('mouse release')
            super(MyWidget, self).mouseReleaseEvent(event)