0

我有这段代码。我尝试的是子类化QGraphicsScene以重新实现它keyPressEvent()的方法。顺便说一下,我不得不typedef为这个东西选择这个特殊的实现(尤其是部分),以满足与其他部分的兼容性问题代码。

按键.h

#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsView>

class Canvas;

typedef struct{
    int id;
    int pid;
    Canvas *qgc;

}scene;

class Canvas: public QGraphicsScene {

    private:
        scene *gc;

    public:
        Canvas(Scene* gc, QWidget *parent=0);

    protected:
        void keyPressEvent(QKeyEvent *e);
};

按键.cpp

#include <QApplication>
#include <QKeyEvent>

#include "keypress.h"

Canvas::Canvas( Scene* gc, QWidget *parent)
    : QGraphicsScene(parent)
{
    this->gc=gc;
}

void Canvas::keyPressEvent(QKeyEvent *event)
{
   if (event->key() == Qt::Key_Escape) {  
   qApp->quit();
   } 
}

主文件

#include <QApplication>
#include "keypress.h"
#include <QGraphicsView>

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);  

  Scene *s;
  Canvas *canvas=new Canvas(s);
  canvas->addText("Hello");

  QGraphicsView view(canvas);
  view.show();
  return app.exec();
}

每当我试图编译这个程序时,我都会出错

In file included from keypress.cpp:4:
keypress.h:29: error: expected ‘)’ before ‘*’ token
keypress.cpp:6: error: expected ‘)’ before ‘*’ token
make: *** [keypress.o] Error 1

我的代码有什么问题?

4

0 回答 0