[SOLVED] QTableWidget: column switch with tab key -- how?
Hello,
I have persistent editor (QTextEdit) opened in each cell and now I would like to be able to just switch the column (I have 2 columns) when the user presses tab key.
setCurrentCell(currentRow(),1-currentColumn());
To do so, I reimplemented eventFilter in QTableWidget and I wait for tab key. There are two problems:
1) I cannot turn off TabKeyNavigation in QTableWidget
2) my code works and then it is ignored, like this: works, works, works, ignored, ignored, works, works, works, ignored, ignored, works...
The result is totally bizarre -- you get 3 times column switch, then "normal" navigation twice, then column switch x3 and so on.
I checked what event I get, and it appears I get key releases all the time, but not presses. Also, it looks like this (inside eventFilter):
( press, release, ) x 3, release, release, press, ...
How can tab key be released twice in a row without pressing?
Tab handling part of evenFilter:
Code:
if (key_event->key()==Qt::Key_Tab ||
key_event->key()==Qt::Key_Backtab)
{
if (_event
->type
()==QEvent::KeyPress) setCurrentCell(currentRow(),currentColumn()==1?0:1);
_event->accept();
return true; // event handled
}
Thank you in advance for enlightening me :-)
have a nice day, bye
Re: QTableWidget: column switch with tab key -- how?
I suggest subclassing QTableWidget and reimplementing QWidget::focusNextPrevChild():
Code:
bool MyTableWidget::focusNextPrevChild(bool next)
{
Q_UNUSED(next);
setCurrentCell(currentRow(), 1 - currentColumn());
return true;
}
Re: QTableWidget: column switch with tab key -- how?
Thank you, strange, but this method is not even called. And it should (?) because it is virtual so it is ok to add my own implementation of it.
In other words -- those changes are transparent.
have a nice day, bye
1 Attachment(s)
Re: QTableWidget: column switch with tab key -- how?
See the attached example.
EDIT: Ahh, now I noticed the problem. QTableWidget::focusNextPrevChild() does not get called when in editing mode.
1 Attachment(s)
Re: QTableWidget: column switch with tab key -- how?
Alright, I have attached an example which works in both, editing and non-editing modes. Reimplementing QAbstractItemView::closeEditor() does the trick in editing mode:
Re: QTableWidget: column switch with tab key -- how?
Wow, this was something :-) I would never guess it -- quite not intuitive. Thank you.