我通过QgraphicsPixmapitem添加了一个字符,现在我想通过按键盘上的箭头键来移动它。但我找不到办法。给出了编译错误。请救救我!
(我的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&)”的匹配函数 移动* player =新移动(Q);
发布于 2015-06-17 16:16:10
你问的问题和你所犯的错误并没有什么共同之处。正如我所看到的,您没有类moveship的任何构造函数,这正是编译器告诉您的。一定有这样的味道:
moveship(const QPixmap & pixmap, QGraphicsItem * parent = 0)当然,正如我所看到的,在类的头部中有一些实现,这是微不足道的:
moveship::moveship(const QPixmap & pixmap, QGraphicsItem * parent = 0)
: QGraphicsItem(pixmap, parent);这是使用c++时应该具备的基本知识。这里没有关于Qt的特别之处。我建议你学点c++。
顺便说一句,建议将重新实现的...event(...)函数放在类的protected部分,并声明它为virtual。
https://stackoverflow.com/questions/30895619
复制相似问题