【发布时间】:2015-06-17 15:13:17
【问题描述】:
我通过 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);
【问题讨论】:
-
什么编译错误?
qDebug()输出是什么? -
@dazewell:编译错误是:“错误:没有匹配函数调用'moveship::moveship(QPixmap&)' moveship * player = new moveship(q); ^"
-
你应该用这个通知更新你的问题,因为它改变了整个问题。你没看到编译器对你说什么吗?查看
moveship构造函数的签名。你没有展示它还是它根本不存在? -
@dazewell 问题已编辑
-
@dazewell 很抱歉我是个菜鸟,但我不知道如何解决这个问题 :(。这是我第一次使用 qt,我把它作为一个学校项目。
标签: c++ qt user-interface graphics qgraphicspixmapitem