捕获编辑QTableView时的ESC键
创始人
2024-12-24 21:00:56
0

要在编辑QTableView时捕获ESC键,可以使用QAbstractItemDelegate类的commitData()函数和closeEditor()函数来实现。

下面是一个示例代码:

from PyQt5.QtWidgets import QApplication, QTableView, QWidget, QVBoxLayout, QLineEdit, QStyledItemDelegate
from PyQt5.QtCore import Qt, QAbstractItemModel, QModelIndex, QVariant
from PyQt5.QtGui import QKeyEvent

class MyDelegate(QStyledItemDelegate):
    def __init__(self, parent=None):
        super().__init__(parent)

    def commitData(self, editor):
        super().commitData(editor)
        # 在提交数据后触发closeEditor函数
        self.closeEditor.emit(editor)

    def closeEditor(self, editor, hint=None):
        if hint == QAbstractItemDelegate.NoHint:
            # 如果没有指定关闭标志,则判断是否为ESC键
            if isinstance(editor, QLineEdit):
                if QApplication.keyboardModifiers() == Qt.NoModifier:
                    event = QKeyEvent(QKeyEvent.KeyPress, Qt.Key_Escape, Qt.NoModifier)
                    QApplication.postEvent(editor, event)

class MyModel(QAbstractItemModel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.dataList = [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]

    def rowCount(self, parent=QModelIndex()):
        return len(self.dataList)

    def columnCount(self, parent=QModelIndex()):
        return len(self.dataList[0])

    def data(self, index, role=Qt.DisplayRole):
        if not index.isValid():
            return QVariant()
        if role == Qt.DisplayRole or role == Qt.EditRole:
            row = index.row()
            col = index.column()
            return QVariant(self.dataList[row][col])
        return QVariant()

    def setData(self, index, value, role=Qt.EditRole):
        if index.isValid() and role == Qt.EditRole:
            row = index.row()
            col = index.column()
            self.dataList[row][col] = value
            return True
        return False

    def flags(self, index):
        return Qt.ItemIsEditable | Qt.ItemIsEnabled

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.tableView = QTableView(self)
        self.model = MyModel(self)
        self.tableView.setModel(self.model)
        self.tableView.setItemDelegate(MyDelegate(self))
        self.tableView.setEditTriggers(QTableView.AllEditTriggers)

        layout = QVBoxLayout(self)
        layout.addWidget(self.tableView)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

在这个示例中,我们创建了一个QStyledItemDelegate的子类MyDelegate,重写了commitData()和closeEditor()函数。在commitData()函数中,我们调用了父类的commitData()函数,然后手动触发closeEditor()函数。在closeEditor()函数中,我们判断关闭标志是否为ESC键,如果是,则发送一个模拟的ESC键事件。

然后,我们创建了一个QAbstractItemModel的子类MyModel,用于提供数据给QTableView。在flags()函数中,我们设置了每个单元格都是可编辑的。最后,我们创建了一个MainWindow类,将QTableView添加到主窗口中,并显示出来。

当你在QTableView中编辑一个单元格时,按下ESC键,编辑框将被关闭。

相关内容

热门资讯

辅助透视(wpk有辅助挂)微扑... 辅助透视(wpk有辅助挂)微扑克有脚本吗(详细辅助新2025教程)原来存在有挂1、很好的工具软件,可...
德扑之星有猫腻!德扑之星软件模... 德扑之星有猫腻!德扑之星软件模拟器,(德扑之星)竟然真的是有挂(详细辅助分享教程);亲们利用一分钟了...
wepoke辅助技巧!GG扑克... wepoke辅助技巧!GG扑克有多假,(AAPOker)切实存在有挂(详细透视科技教程);超受欢迎的...
透视app(WPK透视辅助)德... 透视app(WPK透视辅助)德州ai辅助神器wpk(详细辅助可靠教程)一向真的有挂1、构建自己的微扑...
智星德州菠萝有挂吗!德州最新辅... 智星德州菠萝有挂吗!德州最新辅助器,(wopoker)好像真的有挂(详细辅助详细教程)1、这是跨平台...
辅助透视(德扑ai智能)德州a... 辅助透视(德扑ai智能)德州ai辅助代理(详细辅助玩家教你)原本存在有挂1、下载好辅助软件之后点击打...
德扑之星辅助器购买!wpk德州... 大家肯定在之前德扑之星辅助器购买或者德扑之星辅助器购买中玩过德扑之星辅助器购买!wpk德州透视辅助,...
透视软件(智星德州菠萝有挂吗)... 一、简介了解软件请加微:136704302是一款在线扑克游戏平台,玩家可以在平台上进行多人在线扑克游...
aapoker透明挂!云扑克内... 此外,数据分析德州(aapoker透明挂)辅助神器app还具备辅助透视行为开挂功能,通过对客户aap...
透视工具(aapoker有外挂... 1、透视工具(aapoker有外挂)wepoke软件透明挂视频(详细辅助必备教程)本来是有挂;代表性...