如何在 Qtablewidget Python 中设置自定义键盘按键事件?

How can I set Custom Key board Key press event in Qtablewidget Python?

我有一个由 QTDesigner 生成的 QTableWidget,我想在其中设置一些自定义按键事件。

  1. 当当前单元格处于编辑模式时,如果用户按 Tab 键,它会移动到处于编辑模式的下一个单元格,但我只希望它移动到下一个单元格 selected 而不是编辑模式。

  2. 当当前单元格处于编辑模式时,如果用户按向左、向右、向上、向下 - 它应该再次移动到 select 模式下的相应单元格,而不是处于编辑模式。

如果当前单元格只是 selected 而不是处于编辑模式,那么这两个现在都可以正常工作。但是我该如何设置这个自定义事件呢?

谢谢!!

代码由 UI 设计师生成:

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(428, 285)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(20, 20, 391, 231))
        self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.AnyKeyPressed|QtWidgets.QAbstractItemView.DoubleClicked|QtWidgets.QAbstractItemView.EditKeyPressed|QtWidgets.QAbstractItemView.SelectedClicked)
        self.tableWidget.setRowCount(5)
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setObjectName("tableWidget")
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(2, item)
        self.tableWidget.horizontalHeader().setVisible(True)
        self.tableWidget.verticalHeader().setVisible(False)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "Name"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "Age"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("MainWindow", "City"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我的脚本:

from PyQt5 import QtWidgets, QtCore
from demo import Ui_MainWindow

class DemoTable(QtWidgets.QMainWindow, Ui_MainWindow):                 
    def __init__(self):
        super(DemoTable, self).__init__()
        self.setupUi(self) 

        #KeyPressEvent
        self.tableWidget.keyPressEvent = self.KeyPressed

    def KeyPressed(self,event):
        if event.key() == QtCore.Qt.Key_Left:
            print('Left Key Pressed')
        elif event.key() == QtCore.Qt.Key_Right:
            print('Right Key Pressed')
        elif event.key() == QtCore.Qt.Key_Tab:
            print('Tab Key Pressed')
        return QtWidgets.QTableWidget.keyPressEvent(self.tableWidget, event)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    demowindow = DemoTable()   
    demowindow.show()
    sys.exit(app.exec_())

您可以重新实现 closeEditor() 方法,该方法负责项目编辑完成后项目视图将采取的操作。

在这个例子中,我将像您对按键事件所做的那样覆盖该方法,但我强烈建议您使用子类并在其中实现覆盖,因为代码会更清晰并且可以避免现有函数之间的混淆and/or 覆盖方法。

class DemoTable(QtWidgets.QMainWindow, Ui_MainWindow):                 
    def __init__(self):
        super(DemoTable, self).__init__()
        self.setupUi(self) 

        #KeyPressEvent
        self.tableWidget.keyPressEvent = self.KeyPressed
        self.tableWidget.closeEditor = self.closeEditor

    def closeEditor(self, editor, hint):
        if hint in (QtWidgets.QItemDelegate.EditNextItem, 
            QtWidgets.QItemDelegate.EditPreviousItem):
                # if the hint is to edit the next or previous item, ignore it
                newHint = QtWidgets.QItemDelegate.NoHint
        else:
            newHint = hint

        # call the base implementation with the new hint
        QtWidgets.QTableWidget.closeEditor(self.tableWidget, editor, newHint)

        if hint == QtWidgets.QItemDelegate.EditNextItem:
            # find the next item to focus on
            index = self.tableWidget.moveCursor(self.tableWidget.MoveNext, 
                QtCore.Qt.NoModifier)
        elif hint  == QtWidgets.QItemDelegate.EditPreviousItem:
            # find the previous item to focus on
            index = self.tableWidget.moveCursor(self.tableWidget.MovePrevious, 
                QtCore.Qt.NoModifier)
        else:
            return
        # set the new current item
        self.tableWidget.setCurrentIndex(index)

    # ...

由于您还想使用箭头键移动到项目即使在编辑状态,您只能通过安装检查键盘事件的自定义项目委托来完成此操作在编辑器中。

不过我必须警告你:不要这样做
编辑文本时,左右箭头键始终用于光标导航,绝对不鼓励更改此行为。
它不直观、不自然、不舒服,反对任何常见和预期的行为,并且会让习惯键盘导航的用户非常、非常非常恼火。我以前在一个程序中看到过类似的行为,我可以告诉你,这不好,而且很烦人。

class DelegateYouShouldNotUse(QtWidgets.QStyledItemDelegate):
    def eventFilter(self, source, event):
        if isinstance(source, QtWidgets.QLineEdit) and event.type() == QtCore.QEvent.KeyPress:
            if event.key() == QtCore.Qt.Key_Right:
                # tell the view to store the current data
                self.commitData.emit(source)
                # and to move to the next item (since we've changed the
                # behavior of the closeEditor *slot* of the table, it
                # will only move to the item, without starting editing
                self.closeEditor.emit(source, self.EditNextItem)
                return True
            elif event.key() == QtCore.Qt.Key_Left:
                self.commitData.emit(source)
                self.closeEditor.emit(source, self.EditPreviousItem)
                return True
        return super().eventFilter(source, event)


class DemoTable(QtWidgets.QMainWindow, Ui_MainWindow):                 
    def __init__(self):
        super(DemoTable, self).__init__()
        # ...
        self.veryBadDelegate = DelegateYouShouldNotUse(self.tableWidget)
        self.tableWidget.setItemDelegate(self.veryBadDelegate)

如果不够清楚,你应该不要使用这个:-)