PDA

View Full Version : Keyboard combinations in QTableWidget



enricong
17th October 2011, 21:06
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:


void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key()==Qt::Key_Delete)

// Delete row, this works fine

else if (event->modifiers()&Qt::AltModifier &&
(event->key()==Qt::Key_Up || event->key()==Qt::Key_Down))

// move row up or down, this does not work
}

boudie
17th October 2011, 22:09
Try this:



void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Delete:
qDebug() << "[Del]";
break;
case Qt::Key_Up:
if (event->modifiers() & Qt::AltModifier)
qDebug() << "[Alt + Up]";
break;
case Qt::Key_Down:
if (event->modifiers() & Qt::AltModifier)
qDebug() << "[Alt + Down]";
break;
default: QMainWindow::keyPressEvent(event); // << important!
}

}


Works here; should work for you also...

enricong
18th October 2011, 13:29
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.