我对 c++ 有点陌生,并且在确定为什么我在以下代码中出现段错误时遇到了一些麻烦;
游戏场景.h
#ifndef GAMESCENE_H
#define GAMESCENE_H
#include <QGraphicsScene>
class GameScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GameScene(QObject *parent = 0);
virtual void keyPressEvent(QKeyEvent* );
QGraphicsTextItem* p;
~GameScene();
signals:
public slots:
};
#endif // GAMESCENE_H
游戏场景.cpp
#include "gamescene.h"
#include "QKeyEvent"
#include "QGraphicsTextItem"
#include "QGraphicsRectItem"
#include "QDebug"
#include "QGraphicsScene"
#include "QScopedPointer"
GameScene::GameScene(QObject *parent) :
QGraphicsScene(parent)
{
QGraphicsTextItem* p = new QGraphicsTextItem(QString("HEEEEE"));
p->setFlags(p->ItemIsMovable);
p->moveBy(qreal(500),qreal(500));
addItem(p);
}
void GameScene::keyPressEvent(QKeyEvent *event)
{
qDebug() << (p != NULL);
switch(event->key())
case ( Qt::Key_W ):
{
qreal x, y;
x = qreal(5);
y = qreal(5);
p->moveBy(x,y);
qDebug() << "move up";
}
}
GameScene::~GameScene() {
}
它发生在我的游戏场景对象的 keyPressEvent 方法被调用并尝试访问与 QGraphicsTextItem 指针 p 相关的任何内容时。
我确信它很明显,但任何帮助将不胜感激。