连接到另一个小部件的事件

编程入门 行业动态 更新时间:2024-10-27 19:15:22
本文介绍了连接到另一个小部件的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这很可能是一个重复的问题,但是我不得不问这个问题,因为其他答案对我的情况无济于事,因为我是pyqt的新手(几天前从tkinter转换为).

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)

然后...

def someFunction(event): print(event) ...

我的问题是如何从另一个窗口小部件绑定到特定事件,并将该事件与btn.clicked.connect(function_goes_here)之类的函数连接.

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).

在tkinter中,就像这样:

In tkinter it's something be like this:

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

推荐答案

有多种方法可以实现此目的.侦听给定窗口小部件的所有事件的一种通用方法是在其上安装事件过滤器.所有受保护的功能都有相应的事件类型

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 True return 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.handleKeyRelease def 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.

更多推荐

连接到另一个小部件的事件

本文发布于:2023-11-22 09:56:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   部件   事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!