我通过 QgraphicsPixmapitem 添加了一个字符,现在我想通过按键盘上的箭头键来移动它。但我找不到办法。它给出了编译错误。请帮帮我!
moveship.h(我的 QPixmapItem 的头文件)
#ifndef MOVESHIP_H
#define MOVESHIP_H
#include <QGraphicsPixmapItem>
class moveship: public QGraphicsPixmapItem
{
public:
void keyPressEvent(QKeyEvent *event);
};
#endif // MOVESHIP_H
我只是在检查它是否识别任何按键。
keyPressEvent 的实现:
#include "moveship.h"
#include <QDebug>
void moveship::keyPressEvent(QKeyEvent *event)
{
qDebug() << "moved";
}
我的主要源文件:
#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();
}
请帮忙 :(
编辑:
我得到的编译错误是:
错误:没有匹配函数调用'moveship::moveship(QPixmap&)'
moveship * player = new moveship(q);