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