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

Move a QGraphicsPixmapItem

Scheduled Pinned Locked Moved General and Desktop
graphics scenekeyboardgame
3 Posts 3 Posters 2.7k Views 1 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 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
    • A Offline
      A Offline
      alex_malyu
      wrote on last edited by
      #2

      If there is a compilation error why you did not post it?

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rondog
        wrote on last edited by
        #3

        Possibly the error is from the declaration of keyPressEvent

        protected:
        virtual void keyPressEvent(QKeyEvent *event);
        

        It might be better to have this handled in the parent class. When a graphics item is selected (clicked) store this information and move the item from the parent when the right key presses are seen. You might have a problem with losing focus otherwise.

        1 Reply Last reply
        0

        • Login

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