如何在pyqt5中允许tab键按下事件 [英] How to allow tab key pressing event in pyqt5

查看:118
本文介绍了如何在pyqt5中允许tab键按下事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 button 的 QPushButton,我成功执行以下操作以允许 click 事件:

Assuming that I have a QPushButton named button, I successfully do the following to allow click event:

class UI(object):
    def setupUi(self, Dialog):
        # ...
        self.button.clicked.connect(self.do_something)

    def do_something(self):
        # Something here

我的问题是:当按下 tab 键时,我如何调用 do_something() ?例如,如果我有一个名为 tb_id 的 QlineEdit,在输入一个值并按 Tab 键后,do_something() 方法应该以与 相同的方式调用点击 做上面的.我怎样才能使用 pyqt5 做到这一点?

My question is: how can I call do_something() when the tab key is pressed? For instance, if I have a QlineEdit named tb_id, after entering a value and press tab key, do_something() method should be called in the same way as what clicked does above. How can I do that using pyqt5?

非常感谢.

推荐答案

为了得到你想要的东西,有很多方法,但在通过观察你的代码指出它之前,我看到你已经用 Qt Designer 生成了它,所以代码不应该修改但创建另一个使用该代码的类,因此我将放置基本代码:

To get what you want there are many methods but before pointing it by observing your code I see that you have generated it with Qt Designer so that code should not be modified but create another class that uses that code so I will place the base code:

from PyQt5 import QtCore, QtWidgets

class UI(object):
    def setupUi(self, Dialog):
        self.button = QtWidgets.QPushButton("Press Me")
        lay = QtWidgets.QVBoxLayout(Dialog)
        lay.addWidget(self.button)

class Dialog(QtWidgets.QDialog, UI):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setupUi(self)
        self.button.clicked.connect(self.do_something)

    @QtCore.pyqtSlot()
    def do_something(self):
        print("do_something")

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Dialog()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

另外,我建议您在 Qt 中的事件和信号有什么区别 既然你说的是点击事件,但在 Qt 的世界里,你必须说 点击信号.

Also, I recommend you read the difference between event and signal in the world of Qt in What are the differences between event and signal in Qt since you speak of click event but in the world of Qt one must say clicked signal.

现在进入正题,有以下选项:

Now if going to the point there are the following options:

  • 使用 keyPressEvent:
class Dialog(QtWidgets.QDialog, UI):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setupUi(self)
        self.button.clicked.connect(self.do_something)

    @QtCore.pyqtSlot()
    def do_something(self):
        print("do_something")

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Tab:
            self.do_something()

  • 使用事件过滤器:
  • class Dialog(QtWidgets.QDialog, UI):
        def __init__(self, parent=None):
            super(Dialog, self).__init__(parent)
            self.setupUi(self)
            self.button.clicked.connect(self.do_something)
    
        @QtCore.pyqtSlot()
        def do_something(self):
            print("do_something")
    
        def eventFilter(self, obj, event):
            if obj is self and event.type() == QtCore.QEvent.KeyPress:
                if event.key() == QtCore.Qt.Key_Tab:
                    self.do_something()
            return super(Dialog, self).eventFilter(obj, event)
    

    • 使用 QShorcut 的激活信号:
    • class Dialog(QtWidgets.QDialog, UI):
          def __init__(self, parent=None):
              super(Dialog, self).__init__(parent)
              self.setupUi(self)
              self.button.clicked.connect(self.do_something)
              shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Tab), self)
              shortcut.activated.connect(self.do_something)
      
          @QtCore.pyqtSlot()
          def do_something(self):
              print("do_something")
      

      从前面的方法我更喜欢后者,因为你不需要覆盖任何东西,你可以连接到几个函数.

      From the previous methods I prefer the latter because you do not need to overwrite anything and you can connect to several functions.

      另一方面,只有当焦点在 Qt 窗口中时才会检测到事件.

      On the other hand, only events are detected when the focus is in the Qt window.

      这篇关于如何在pyqt5中允许tab键按下事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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