PDA

View Full Version : [SOLVED] QTableWidget: column switch with tab key -- how?



macias
12th August 2007, 11:25
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:


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

jpn
12th August 2007, 12:07
I suggest subclassing QTableWidget and reimplementing QWidget::focusNextPrevChild():


bool MyTableWidget::focusNextPrevChild(bool next)
{
Q_UNUSED(next);
setCurrentCell(currentRow(), 1 - currentColumn());
return true;
}

macias
12th August 2007, 12:28
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

jpn
12th August 2007, 12:38
See the attached example.

EDIT: Ahh, now I noticed the problem. QTableWidget::focusNextPrevChild() does not get called when in editing mode.

jpn
12th August 2007, 12:47
Alright, I have attached an example which works in both, editing and non-editing modes. Reimplementing QAbstractItemView::closeEditor() does the trick in editing mode:


void closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint)
{
if (hint == QAbstractItemDelegate::EditNextItem && currentColumn() == 1)
hint = QAbstractItemDelegate::EditPreviousItem;
else if (hint == QAbstractItemDelegate::EditPreviousItem && currentColumn() == 0)
hint = QAbstractItemDelegate::EditNextItem;
QTableWidget::closeEditor(editor, hint);
}

macias
12th August 2007, 13:01
Wow, this was something :-) I would never guess it -- quite not intuitive. Thank you.