How to intercept keyboard in QT?
-
Let's imagine that I have a picture, I want to move it by pressing a key on the keyboard. Perhaps I need to use slots, but how to let the computer know that this or that key is pressed: DC++ Juliette Santos, Jul 11, 2019
-
stackoverflow.com/questions/12558988/qt-keypress-eventLucas Esparza
-
All graphical Qt classes in the protected area have different -event methods.
by default event is forwarded to the child class. You need to override the appropriate method. For everything else here: doc.qt.io/qt-5/eventsandfilters .htmlAurora Crawford -
We add the keyPressEvent method to our class, and we ourselves call the corresponding methods / slots that will move the image:
void MainWindow::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Up:
_image->up();
break;
case Qt::Key_Down:
_image->down();
break;
case Qt::Key_Left:
_image->left();
break;
case Qt::Key_Right:
_image->right();
break;
}
}Dominick Moreno
3 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!