Results 1 to 3 of 3

Thread: Keyboard combinations in QTableWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Keyboard combinations in QTableWidget

    I have a QTableWidget and I would like to add some keyboard shortcuts so the user can move row items up and down.

    I already have a keyboard shortcut for the "delete" button to delete a row working.

    I tried to do a shortcut on the up and down arrow keys, but it did not work. I figure this is because QTableWidget is using those keys for navigation.

    Therefore I wanted to use Alt+Key_Up and Alt+Key_Down, but that does not seem to work either. From the debugger, it seems like the event is being called when I press the Alt key before I have a chance to press up.

    Here is some of my code:
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3. if (event->key()==Qt::Key_Delete)
    4.  
    5. // Delete row, this works fine
    6.  
    7. else if (event->modifiers()&Qt::AltModifier &&
    8. (event->key()==Qt::Key_Up || event->key()==Qt::Key_Down))
    9.  
    10. // move row up or down, this does not work
    11. }
    To copy to clipboard, switch view to plain text mode 
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Keyboard combinations in QTableWidget

    Try this:

    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3. switch (event->key())
    4. {
    5. case Qt::Key_Delete:
    6. qDebug() << "[Del]";
    7. break;
    8. case Qt::Key_Up:
    9. if (event->modifiers() & Qt::AltModifier)
    10. qDebug() << "[Alt + Up]";
    11. break;
    12. case Qt::Key_Down:
    13. if (event->modifiers() & Qt::AltModifier)
    14. qDebug() << "[Alt + Down]";
    15. break;
    16. default: QMainWindow::keyPressEvent(event); // << important!
    17. }
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    Works here; should work for you also...

  3. #3
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Keyboard combinations in QTableWidget

    This doesn't work for me, infact it never gets called.

    I think this is because QTableWidget "intercepts" the Key_Up and Key_Down events before it gets to me since it uses those keys for navigation.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

Similar Threads

  1. Replies: 4
    Last Post: 16th September 2011, 02:10
  2. Replies: 26
    Last Post: 12th April 2011, 14:06
  3. Replies: 1
    Last Post: 6th January 2011, 04:19
  4. Capture key & key combinations from keyboard
    By aikidorb in forum Qt Programming
    Replies: 3
    Last Post: 5th June 2010, 13:13
  5. Replies: 2
    Last Post: 22nd July 2007, 19:21

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
  •  
Qt is a trademark of The Qt Company.