0

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>

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 :(

Edit:

The compilation error which I get is:

error: no matching function for call to 'moveship::moveship(QPixmap&)'
moveship * player = new moveship(q);

Swapnil Pandey
  • 577
  • 3
  • 8
  • 25
  • 1
    What compilation error? What is `qDebug()` output? – dazewell Jun 17 '15 at 15:51
  • @dazewell: The compilation error is : "error: no matching function for call to 'moveship::moveship(QPixmap&)' moveship * player = new moveship(q); ^" – Swapnil Pandey Jun 17 '15 at 15:56
  • you shoud have updated your question with this notice, because it changes the whole question. Don't you see what compiler says to you? Check out the signature of your `moveship` constructor. You didn't show it or it doesn't exist at all? – dazewell Jun 17 '15 at 15:59
  • @dazewell Question edited – Swapnil Pandey Jun 17 '15 at 16:04
  • @dazewell sorry for being a noob but I do not know how to solve this problem :(. This is my first experience with qt and I've got this as a school project. – Swapnil Pandey Jun 17 '15 at 16:05

1 Answers1

0

Well, there's nothing common in the question you ask and the error you get. As I see, you don't have any constructor of your class moveship, and that's exactly what compiler is telling to you. There's got to be smth like:

moveship(const QPixmap & pixmap, QGraphicsItem * parent = 0)

in the header of your class with some implementation, of course and as I see, it's trivial:

moveship::moveship(const QPixmap & pixmap, QGraphicsItem * parent = 0) 
                 : QGraphicsItem(pixmap, parent);

That's the base knowledge you should have when using c++. Here's nothing special about Qt. I advise you to learn c++ a bit.

BTW, reimplemented ...event(...) functions is recommended to place in the protected part of the class and also declare it to be virtual.

dazewell
  • 396
  • 2
  • 17
  • Wouldn't the parent constructor be called automagically ? My guess was the argument was wrong... my guess was wrong... – Thalia Jun 17 '15 at 16:26
  • @Thalia no, it won't. There will be default constructor created `moveship();`, and it doesn't have QPixmap as an argument and that's what causes the error. My implementation **does** call parent's constructor with given to the child class parameters. – dazewell Jun 17 '15 at 16:32
  • thanks a lot @dazewell. I know that was quite stupid on my part. Apologies. – Swapnil Pandey Jun 17 '15 at 19:57