PDA

View Full Version : Forward key event to other widget?



Boron
6th February 2012, 09:43
Hello,

I have a QTableWidget based class with an reimplemented keyPressEvent(...) method.

Unfortunately the window that holds the tableWidget does not react on the Tab-key any more (catchword: tab order). the tab order works as long as the tableWidget is not reached. As soon as the tabWidget has the focus the Tab-key only switches from one table cell to the next.
The tab order of the parent widget is "stopped" in from this moment on.

How can I make the tabWidget ignore the Tab-key and leave it to the surrounding widget that hold the tabWidget?

My implementation of the keyPressEvent(). No magic so far:

void MyTableWidget::keyPressEvent( QKeyEvent* event )
{
switch( event->key() )
{
case Qt::Key_Home:
setCurrentCell(0, 0);
break;
default:
QTableWidget::keyPressEvent(event);
break;
}
}

wysota
6th February 2012, 14:18
Reimplement QObject::event() for your table widget (or its viewport(), try both) and handle Qt::Key_Tab there. Alternatively reimplement QAbstractItemView::focusNextPrevChild() and call implementation of the method from QWidget there.

Boron
6th February 2012, 15:25
Thank you.
The second idea was pretty easy.

For those having the same "problem" as I had: Here is what I have done:

bool MyTableWidget::focusNextPrevChild( bool next )
{
QWidget::focusNextPrevChild(next);
return false; // a "true" also works
}

wysota
6th February 2012, 15:55
How about just:

bool MyTableWidget::focusNextPrevChild( bool next )
{
return QWidget::focusNextPrevChild(next);
}