Move a QGraphicsPixmapItem through keyboard

  • I have a a character added via QgraphicsPixmapitem and now I want to move it by pressing the arrow keys on the keyboard. But I can't find a way. It gives compilation error. Help me please!

    moveship.h (header file for my QPixmapItem)

        #ifndef MOVESHIP_H
        #define MOVESHIP_H
        #include <QGraphicsPixmapItem>
        class moveship: public QGraphicsPixmapItem
            void keyPressEvent(QKeyEvent *event);
        #endif // MOVESHIP_H

    I am just checking if it recognises any key press or not.

    Implementation of keyPressEvent:

      #include "moveship.h"
      #include <QDebug>
      void moveship::keyPressEvent(QKeyEvent *event)
          qDebug() << "moved";

    My main source file:

        #include <moveship.h>
        int main(int argc, char *argv[])
            QApplication a(argc, argv);
            QGraphicsScene * scene = new QGraphicsScene();
            QGraphicsView * view = new QGraphicsView(scene);
            QPixmap q = QPixmap("://images/player.png");
                moveship * player = new moveship(q);
            return a.exec();

    Please help :(

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What exact error are you getting ?

  • Lifetime Qt Champion

    Just saw that you already posted the same question here

    Please, don't post the same questions multiple times in different sub forum. One is enough.

    Closing it.

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.