Answer the question
In order to leave comments, you need to log in
How to intercept the keyboard in QT?
Imagine that I have a picture, I want to move it by pressing a key on the keyboard. Maybe I need to use slots, but how do I let the computer know that this or that key is pressed: D
Answer the question
In order to leave comments, you need to log in
All Qt graphics classes in protected scope have different -event methods.
by default, event is passed to the child class. You need to override the appropriate method. For everything else here: doc.qt.io/qt-5/eventsandfilters.html
We add the keyPressEvent method to our class, and we ourselves call the appropriate 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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question