Unsolved Loop in taborder in a QTableView
-
Hello all,
I have a problem with taborder in a QTableView. Indeed, when i browse between my widgets with TAB, if a widget is a QTableView, the focus enters in it and loop over the table cells. The focus is trapped inside the QTableView. TAB just focus the first cell after the last cell.
I would like the focus to escape the QTableView after tabbing on the last cell.
An idea ?
Thanks !
Ben -
@Ben35
you need to filter the Qt::Key_Tab (and Backtab) key events to your table view.
If the current index is currently positioned at the end you need to call focusNextChild() / focusPreviousChild()Alternatively you can sublass your table view and reimplement moveCursor():
class MyTable : public QTableView { virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) { QModelIndex index = QTableView::moveCursor(cursorAction, modifiers); switch( cursorAction ) { case MoveNext: //TAB case MovePrevious: //BACKTAB { if( doNotFocusNextItem ) // <<<<<<<<<<< { index = QModelIndex(); // do not update the current index (see QAbstractItemView::keyPressEvent() handler) this->focusNextPrevChild( cursorAction == MoveNext ); //let Qt focus next widget } } break; } return index; }
Haven't tested it though.
-
Thanks for your reply,
Before your answer, i tried this (i override the focusNextPrevChild method of my tableview) :
bool focusNextPrevChild(bool next) override final
{
auto index = currentIndex();
if (index.row() == model()->rowCount() - 1 && index.column() == model()->columnCount() - 1) {
return QWidget::focusNextPrevChild(next);
}
return QTableView::focusNextPrevChild(next);
}It seems to have the behaviour i expected. Do you think it's OK ?
-
@Ben35
oh... i haven't seen that the actual keyPress event is generated in QAbstractItemView::focusNextPrevChild() and sent to the table view widget which then calls moveCursor(),So my proposed solution (using moveCursor()) wouldn't work anyway (as it is).
Yours should be fine.
-
OK. It seems that there is a problem too with my solution. It works when i tab after the last cell in a "focus/selection mode", but if i am in a "cell edit mode" (in which a tab open the editor of the next cell), then the loop is still there ...
-
@Ben35
in this case you also need to integrate my moveCursor() fix.
Maybe you should anyway fix it this way (with your tweak to call QWidget's focusNextPrevChild() instead)Watch out for endless recursions!