PDA

View Full Version : QTableView focus issue



gemidjy
19th February 2008, 14:07
Hello, I am having an issue with QTableView object that I use in my application. I implement the keyPressEvent(QKeyEvent *event) method to get the event when a key is pressed. Thus, I want to get findout when the Enter/Return key is pressed while the application's focus is in the QTableView object. So I do:



void jeamMain::keyPressEvent(QKeyEvent *e)
{
switch(e->key()) {
case Qt::Key_Return:
if(jeamTableView->hasFocus())
QMessageBox::information(this, "", "Enter pressed and focus on table");
else
return;
break;
}
}


But the QTableView object (jeamTableView) seems that it doesn't accept events from the keyboard. It's like ignoring the Return key. If I remove the "if" construction that checks if the focus is in the jeamTableView object, and put the focus in QLineEdit object and press Return, I get the QMessageBox with the text "Enter pressed and focus on table". Otherwise, jeamTableView doesn't react on the keyPressEvent.

The focus policy set for the jeamTableView is StrongFocus.
Thanks in advance

jpn
19th February 2008, 15:00
How about using QShortcut?

gemidjy
19th February 2008, 15:22
How about using QShortcut?

Yes, that is a workaround for the problem, but I actually want to learn why tableView doesn't accept the event.

jpn
19th February 2008, 15:35
I don't understand what are you trying to do. Ignored events get propagated to parent widget but presumably item views handle their key presses so key press events never reach the parent widget. If you want to catch events of the table view you should reimplement its event handler or install an event filter on it. Don't expect to receive corresponding events in parent's event handler.

Using QShortcut is not a workaround but way more elegant and simpler way to handle it...

gemidjy
19th February 2008, 15:51
I don't understand what are you trying to do. Ignored events get propagated to parent widget but presumably item views handle their key presses so key press events never reach the parent widget. If you want to catch events of the table view you should reimplement its event handler or install an event filter on it. Don't expect to receive corresponding events in parent's event handler.

Using QShortcut is not a workaround but way more elegant and simpler way to handle it...

Ok I will reconsider using QShortcut :)
thanks