Make QTableView behave identical for Key_Up and Key_Tab when there are delegates
Unsolved
General and Desktop
-
I have a QTable in which one column sometimes, i.e. for some rows, have a delegate editor of type QLineEdit. User can only select entire row. When one QLineEdit have focus and the user presses Key_Up/Key_Down the previous/next row is selected and previous/next QLineEdit automatically gets focus. How can I get the same behaviour for tab? I've tried to capture the key events in the table view, i.e.
void ScalarTableTableView::keyPressEvent(QKeyEvent *event) { // Change tab to arrow up/down to be able to move to next row with one key. if(event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab) { QKeyEvent newEvent(event->type(), event->key() == Qt::Key_Tab ? Qt::Key_Down : Qt::Key_Up, event->modifiers(), event->nativeScanCode(), event->nativeModifiers(), event->nativeVirtualKey(), event->text(), event->isAutoRepeat(), event->count()); event->accept(); MoveBetweenViewsWithDragAndDropTableView::keyPressEvent(&newEvent); } else { MoveBetweenViewsWithDragAndDropTableView::keyPressEvent(event); } }
This works for selecting the previous/next row but the QLineEdit doesn't get focus. Do I need to set focus manually? If so, how can I get the delegate QLineEdit for a certain row?
-
@olowo726 You can do the move directly:
void ScalarTableTableView::keyPressEvent(QKeyEvent *event) { if(event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab) { QModelIndex newCurrent = moveCursor(event->key() == Qt::Key_Tab ? MoveDown: MoveUp, event->modifiers()); QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event); selectionModel()->setCurrentIndex(newCurrent, command); return; } QTableView::keyPressEvent(event); }