开发者

PyQt QTableWidget keyboard events while editing

开发者 https://www.devze.com 2023-01-19 22:24 出处:网络
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

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开发者_如何学Go docs, but can't seem to find out how.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消