Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Move a QGraphicsPixmapItem through keyboard
Forum Updated to NodeBB v4.3 + New Features

Move a QGraphicsPixmapItem through keyboard

Scheduled Pinned Locked Moved Game Development
graphics scenegameqgraphicsitemkeyboardmoving
3 Posts 2 Posters 2.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swapnilp96
    wrote on 17 Jun 2015, 15:31 last edited by
    #1

    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
        {
            public:
            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<QApplication>
        #include<QGraphicsScene>
        #include<QGraphicsView>
        #include<QGraphicsPixmapItem>
        #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");
            if(q.isNull())
            {
                printf("null\n");
            }
            else
            {
                moveship * player = new moveship(q);
                scene->addItem(player);
            }
        
            view->resize(500,500);
            view->show();
        
            return a.exec();
        }
    

    Please help :(

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Jun 2015, 23:44 last edited by
      #2

      Hi and welcome to devnet,

      What exact error are you getting ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 17 Jun 2015, 23:47 last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        1/3

        17 Jun 2015, 15:31

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved