Thomasedv
Thomasedv

Reputation: 402

QTableWidget, Stop editing of QTableWidgetItem

I have a QTableWidget with lots of items. I wanted to make it so that when i press the Return key (Same as Qt.Key_Return) while editing a cell, it will select and edit the next cell. However, if i call the nextrow function, it will not do anything, if i'm already editing a QTableWidgetItem already. It does work when nothing is being edited, and it will edit the selected row.

self.table = QTablewidget()
... # Populate widget with QTableWidgetItems

def nextrow(self, row)
    column = 1
    self.table.editItem(self.table.item(row, column))

I see no way to actually disable that edit mode, and it completely ignores everything from setting the text to something, disabling the edit permission, because i'm already editing it. And as far as i looked, i couldn't find any method to do it either.

Upvotes: 0

Views: 1008

Answers (1)

MalloyDelacroix
MalloyDelacroix

Reputation: 2293

You need to capture the key press event from the QTableWidget. To do this you will need to subclass the QTableWidget and implement the change row function from within.

self.table = CustomTableWidget()
... # Populate widget with QTableWidgetItems

class CustomTableWidget(QtWidgets.QTableWidget):

    def __init__(self):
        super().__init__()

    def keyPressEvent(self, event):
        key = event.key()
        if key == QtCore.Qt.Key_Return:
            self.select_next_row()

    def select_next_row(self):
        self.setCurrentCell(self.currentRow() + 1, self.currentColumn())
        self.edit(self.currentIndex())

Upvotes: 2

Related Questions