Implement the virtual function keyPressEvent
from base class QWidget
:
void YourWidget::keyPressEvent(QKeyEvent *event)
{
if( event->key() == Qt::Key_F2 )
{
emit F2isPressed(/* some signature */); // connected elsewhere
}
}
OP Note: While this answer doesn't quite work correctly for the particular case I was trying to figure out (the F2 key being pressed), it does work for pretty much any other key that will trigger that function (e.g the other Fx keys, etc.). It turns out F2 is a really weird case for Qt and requires some workaround to get to work. To see how the original post was solved by myself and the poster of this question, you can look in the chat here. I'm going to mark this answer as accepted, because although it doesn't work for this one particular case, it is in general the correct answer for this type of problem.