Results 1 to 5 of 5

Thread: QTableView focus issue

  1. #1
    Join Date
    Feb 2008
    Posts
    16
    Thanks
    3

    Default QTableView focus issue

    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:

    Qt Code:
    1. void jeamMain::keyPressEvent(QKeyEvent *e)
    2. {
    3. switch(e->key()) {
    4. case Qt::Key_Return:
    5. if(jeamTableView->hasFocus())
    6. QMessageBox::information(this, "", "Enter pressed and focus on table");
    7. else
    8. return;
    9. break;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView focus issue

    How about using QShortcut?
    J-P Nurmi

  3. #3
    Join Date
    Feb 2008
    Posts
    16
    Thanks
    3

    Default Re: QTableView focus issue

    Quote Originally Posted by jpn View Post
    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView focus issue

    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...
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    gemidjy (19th February 2008)

  6. #5
    Join Date
    Feb 2008
    Posts
    16
    Thanks
    3

    Default Re: QTableView focus issue

    Quote Originally Posted by jpn View Post
    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

Similar Threads

  1. Force focus to a QTabWidget page's widget
    By thomaspu in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2008, 07:54
  2. focus issue
    By samirg in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2007, 02:29
  3. Tab/Enter focus problem
    By b1 in forum Qt Programming
    Replies: 4
    Last Post: 24th October 2006, 00:34
  4. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 14:49
  5. QTableView number of rows and scrollbar issue
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 07:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.