Connecting to events of another widget

2024/10/18 12:48:31

This is most likely a duplicate question, but I have to ask it because other answers aren't helping in my case, since I am new to pyqt (switched from tkinter few days ago).

I am wondering if is it possible to connect to an event of a widget like this:

 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 is how to bind to a specific event from another widget, and connect that event with a function - like btn.clicked.connect(function_goes_here).

In tkinter it's something be like this:

self.Entry.bind("<KeyRelease-a>", lambda event: someFunction(event))
Answer

There are a number of different ways to achieve this. A generic way to listen to all events for a given widget, is to install an event-filter on it. All protected functions have a corresponding event type that can be accessed in this way:

class MainmWindow(QMainWindow):def __init__(self):...self.lineEdit = QLineEdit(self.frame)self.lineEdit.installEventFilter(self)def eventFilter(self, source, event):if source is self.lineEdit:if event.type() == QEvent.KeyRelease:print('key release:', event.key())# the following line will eat the key event# return Truereturn super(MainmWindow, self).eventFilter(source, event)

Alternatively, you can sub-class the widget, re-implement the relevant event handler, and emit a custom signal:

class LineEdit(QLineEdit):keyReleased = pyqtSignal(int)def keyReleaseEvent(self, event):self.keyReleased.emit(event.key())super(LineEdit, self).keyReleaseEvent(event)class MainmWindow(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 this is to overwrite the method directly:

class MainmWindow(QMainWindow):def __init__(self):...self.lineEdit = QLineEdit(self.frame)self.lineEdit.keyReleaseEvent = self.handleKeyReleasedef handleKeyRelease(self, event):print('key release:', event.key())QLineEdit.keyReleaseEvent(self.lineEdit, event)

Note that if you don't want to invoke the default event handling, you can omit the call to the base-class method.

https://en.xdnf.cn/q/73203.html

Related Q&A

Diff multidimensional dictionaries in python

I have two dictionariesa = {home: {name: Team1, score: 0}, away: {name: Team2, score: 0}} b = {home: {name: Team1, score: 2}, away: {name: Team2, score: 0}}The keys never change but I want to get that …

Pandas DatetimeIndex vs to_datetime discrepancies

Im trying to convert a Pandas Series of epoch timestamps to human-readable times. There are at least two obvious ways to do this: pd.DatetimeIndex and pd.to_datetime(). They seem to work in quite dif…

Slicing a circle in equal segments, Python

I have a set of close of 10,000 points on the sky. They are plotted using the RA (right ascension) and DEC (declination) on the sky. When plotted, they take the shape of a circle. What I would like to …

Pyautogui screenshot. Where does it go? How to save and find later?

I am learning from Al Sweigarts you tube video for automating the boring stuff. I got to the part of taking screenshots. He didnt really explain in his video so I tested things out. I found that it tak…

How to get pip to point to newer version of Python

I have two versions of Python installed on my centOS server. [ethan@demo ~]$ python2.6 --version Python 2.6.6 [ehtan@demo ~]$ python --version Python 2.7.3The older version (2.6) is required by some es…

Connect JS client with Python server

Im relatively new to JS and Python, so this is probably a beginners question. Im trying to send a string from a JS client to Python Server (and then send the string to another Python client).This is my…

Pip does not acknowledge Cython

I just installed pip and Python via home-brew on a fresh Mac OS installation.First of all, my pip is not installing dependencies at all - which forces me to re-run pip install tables 3 times and every …

Is it me or is pygame.key.get_pressed() not working?

okay, so I am making a basic space-ship game.I cant get rotation to work because it scrambles the bitmap, but thats for another question.Should I even use a gif? any other filetype suggestions?back t…

Find out if a python script is running in IDLE or terminal/command prompt

Is there a way to find out if the python script is running in the IDLE interpreter or the terminal?Works cross-platform if possible, or if needed a different way for each platform.Work with Python 2 a…

How to map coordinates in AxesImage to coordinates in saved image file?

I use matplotlib to display a matrix of numbers as an image, attach labels along the axes, and save the plot to a PNG file. For the purpose of creating an HTML image map, I need to know the pixel coor…