我需要拦截用户在QTableWidget
中编辑单元格时发出的关键事件。有一个简单的方法吗?我在考虑安装事件过滤器,但我不知道如何获得对编辑器窗口小部件的引用。或者我是否需要以某种方式重新实现QItemDelegate
?这似乎对我来说太过分了,不知道如何做到这一点,究竟需要重新实现什么。或者还有另一种方式吗?
我需要这个,因为例如当用户按下左/右光标键时,我想跳到左/右的下一个单元格。
答案 0 :(得分:2)
似乎我终于想出来了,它完全符合我的需要。这是使用覆盖QStyledItemDelegate
的PySide(C ++类似)的解决方案。在我看来,一开始有点矫枉过正,但并没有那么糟糕。
fromPySide import QtCore, QtGui
class LineEditDelegate(QtGui.QStyledItemDelegate):
moveCurrentCellBy = QtCore.Signal(int, int)
def __init__(self, parent=None):
super(LineEditDelegate, self).__init__(parent)
def createEditor(self, parent, option, index):
self.editor = QtGui.QLineEdit(parent)
self.editor.setFrame(False)
self.editor.installEventFilter(self)
return self.editor
def setEditorData(self, editor, index):
value = index.model().data(index, QtCore.Qt.EditRole)
editor.setText(value)
def setModelData(self, editor, model, index):
value = editor.text()
model.setData(index, value, QtCore.Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
def eventFilter(self, target, event):
if target is self.editor:
if event.type() == QtCore.QEvent.KeyPress:
moveCell, row, column = False, 0, 0
if event.key() in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Down):
moveCell, row, column = True, 1, 0
if event.key() in (QtCore.Qt.Key_Right, QtCore.Qt.Key_Tab):
moveCell, row, column = True, 0, 1
if event.key() in (QtCore.Qt.Key_Left, QtCore.Qt.Key_Backtab):
moveCell, row, column = True, 0, -1
if event.key() == QtCore.Qt.Key_Up:
moveCell, row, column = True, -1, 0
if moveCell:
self.commitData.emit(self.editor)
self.closeEditor.emit(self.editor, QtGui.QAbstractItemDelegate.NoHint)
self.moveCurrentCellBy.emit(row, column)
return True
return False
class TableWidget(QtGui.QTableWidget):
def __init__(self, parent=None):
super(TableWidget, self).__init__(parent)
delegate = LineEditDelegate()
delegate.moveCurrentCellBy.connect(self.moveCurrentCellBy)
self.setItemDelegate(delegate)
def moveCurrentCellBy(self, rowStep, columnStep):
row = self.currentRow() + rowStep
column = self.currentColumn() + columnStep
if row >= self.rowCount():
self.setRowCount(row + 1)
if column >= self.columnCount():
self.setColumnCount(column + 1)
self.setCurrentCell(row, column)
答案 1 :(得分:0)
或者你可以使用像这样的cellClicked信号:
connect(ui->tbControl,SIGNAL(cellClicked(int,int)),this,SLOT(detectCellSelectionGraph(int,int)));