编辑时的 PyQt QTableWidget 键盘事件

发布于 2024-09-27 08:00:34 字数 130 浏览 3 评论 0原文

我想以与 MS Excel 类似的方式导航 QTableWidget

例如,当用户在编辑单元格时按右箭头键,编辑将完成,并且右侧的下一个单元格将被选择。我已经搜索了 Qt 文档,但似乎无法找到具体方法。

I want to navigate QTableWidget in a similar way to MS Excel.

For example, when the user presses the right arrow key while editing a cell, the editing will finish and the next cell to the right will be selected. I have searched the Qt docs, but can't seem to find out how.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2024-10-04 08:00:34
from PyQt4.QtCore import QEvent, Qt
from PyQt4.QtGui import QTableWidget, QWidget, QVBoxLayout, QApplication


class MyTableWidget(QTableWidget):
    def __init__(self):
        QTableWidget.__init__(self)

        self.keys = [Qt.Key_Left,
                     Qt.Key_Right]

        # We need this to allow navigating without editing
        self.catch = False 

    def focusInEvent(self, event):
        self.catch = False
        return QTableWidget.focusInEvent(self, event)

    def focusOutEvent(self, event):
        self.catch = True
        return QTableWidget.focusOutEvent(self, event)    

    def event(self, event):
        if self.catch and event.type() == QEvent.KeyRelease and event.key() in self.keys:
            self._moveCursor(event.key())

        return QTableWidget.event(self, event)

    def keyPressEvent(self, event):
        if not self.catch:
            return QTableWidget.keyPressEvent(self, event)

        self._moveCursor(event.key())


    def _moveCursor(self, key):
        row = self.currentRow()
        col = self.currentColumn()

        if key == Qt.Key_Left and col > 0:
            col -= 1

        elif key == Qt.Key_Right and col < self.columnCount():
            col += 1

        elif key == Qt.Key_Up and row > 0:
            row -= 1

        elif key == Qt.Key_Down and row < self.rowCount():
            row += 1

        else:
            return

        self.setCurrentCell(row, col)
        self.edit(self.currentIndex())


class Widget(QWidget): 
    def __init__(self, parent=None): 
        QWidget.__init__(self)            
        tableWidget = MyTableWidget()
        tableWidget.setRowCount(10)
        tableWidget.setColumnCount(10)

        layout = QVBoxLayout() 
        layout.addWidget(tableWidget)  
        self.setLayout(layout) 

app = QApplication([]) 
widget = Widget() 
widget.show() 
app.exec_()

不确定你是否还需要这个,但就这样吧。

from PyQt4.QtCore import QEvent, Qt
from PyQt4.QtGui import QTableWidget, QWidget, QVBoxLayout, QApplication


class MyTableWidget(QTableWidget):
    def __init__(self):
        QTableWidget.__init__(self)

        self.keys = [Qt.Key_Left,
                     Qt.Key_Right]

        # We need this to allow navigating without editing
        self.catch = False 

    def focusInEvent(self, event):
        self.catch = False
        return QTableWidget.focusInEvent(self, event)

    def focusOutEvent(self, event):
        self.catch = True
        return QTableWidget.focusOutEvent(self, event)    

    def event(self, event):
        if self.catch and event.type() == QEvent.KeyRelease and event.key() in self.keys:
            self._moveCursor(event.key())

        return QTableWidget.event(self, event)

    def keyPressEvent(self, event):
        if not self.catch:
            return QTableWidget.keyPressEvent(self, event)

        self._moveCursor(event.key())


    def _moveCursor(self, key):
        row = self.currentRow()
        col = self.currentColumn()

        if key == Qt.Key_Left and col > 0:
            col -= 1

        elif key == Qt.Key_Right and col < self.columnCount():
            col += 1

        elif key == Qt.Key_Up and row > 0:
            row -= 1

        elif key == Qt.Key_Down and row < self.rowCount():
            row += 1

        else:
            return

        self.setCurrentCell(row, col)
        self.edit(self.currentIndex())


class Widget(QWidget): 
    def __init__(self, parent=None): 
        QWidget.__init__(self)            
        tableWidget = MyTableWidget()
        tableWidget.setRowCount(10)
        tableWidget.setColumnCount(10)

        layout = QVBoxLayout() 
        layout.addWidget(tableWidget)  
        self.setLayout(layout) 

app = QApplication([]) 
widget = Widget() 
widget.show() 
app.exec_()

Not sure if you still need this, but here goes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文