python 3.x - Connecting keyRelease events to PyQt4 widgets -
this duplicate question, have ask because other answers aren't helping in case since new pyqt (switched tkinter few days ago).
i wondering possible connect event widget >
self.lineedit = qtgui.qlineedit(self.frame) self.lineedit.keyreleaseevent(lambda: somefunction(qtcore.qt.key_a )) self.lineedit.setobjectname(_fromutf8("lineedit")) self.horizontallayout.addwidget(self.lineedit)
and then...
def somefunction(event): print(event) ...
my question how bind specific event widget, , connect event function... btn.clicked.connect(function_goes_here)
in tkinter it's :
self.entry.bind("<keyrelease-a>", lambda event: somefunction(event))
simple working example great, if possible
there number of different ways achieve this. generic way listen all events given widget, install event-filter on it:
class mainmwindow(qtgui.qmainwindow): def __init__(self): ... self.lineedit = qtgui.qlineedit(self.frame) self.lineedit.installeventfilter(self) def eventfilter(self, source, event): if source self.lineedit: if event.type() == qtcore.qevent.keyrelease: print('key release:', event.key()) # following line eat key event # return true return super(mainmwindow, self).eventfilter(source, event)
alternatively, can sub-class widget, re-implement relevant event handler, , emit custom signal:
class lineedit(qtgui.qlineedit): keyreleased = qtcore.pyqtsignal(int) def keyreleaseevent(self, event): self.keyreleased.emit(event.key()) super(lineedit, self).keyreleaseevent(event) class mainmwindow(qtgui.qmainwindow): def __init__(self): ... self.lineedit = lineedit(self.frame) self.lineedit.keyreleased.connect(self.handlekeyrelease) def handlekeyrelease(self, key): print('key release:' key)
a more hackish variation on overwrite method directly:
class mainmwindow(qtgui.qmainwindow): def __init__(self): ... self.lineedit = qtgui.qlineedit(self.frame) self.lineedit.keyreleaseevent = self.handlekeyrelease def handlekeyrelease(self, event): print('key release:', event.key()) qtgui.qlineedit.keyreleaseevent(self.lineedit, event)
note if don't want invoke default event handling, can omit call base-class method.
Comments
Post a Comment