D
D
DenY22015-06-29 14:07:06
Programming
DenY2, 2015-06-29 14:07:06

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

3 answer(s)
A
Alexander Ruchkin, 2015-06-29
@VoidEx

stackoverflow.com/questions/12558988/qt-keypress-event

D
DancingOnWater, 2015-06-29
@DancingOnWater

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

J
Jacob E, 2015-06-30
@Zifix

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 question

Ask a Question

731 491 924 answers to any question