连接到另一个小部件的事件 [英] Connecting to events of another widget

查看:72
本文介绍了连接到另一个小部件的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很可能是一个重复的问题,但是我不得不问这个问题,因为其他答案对我的情况无济于事,因为我是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.

这篇关于连接到另一个小部件的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆