class MyGraphicsView : public QGraphicsView
{
Q_OBJECT //这行与下面的发射信号的行是联动的
public:
MyGraphicsView(QGraphicsScene *scene) : QGraphicsView(scene)
{
}
signals:
void testSignal();
protected:
void resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
fitInView(sceneRect(), Qt::KeepAspectRatio);
}
virtual void mousePressEvent ( QMouseEvent * mouseEvent )
{
qDebug()<<"mousePressEvent";
emit testSignal(); //这行与上面的Q_OBJECT宏定义是联动的
QGraphicsView::mousePressEvent(mouseEvent);
}
};
这个代码是我修改了 QT4.7.4的两个例子,animatedtiles和dragdroprobot,结果第一个例子编译通过,第二个例子链接失败,错误信息:
debug/main.o:main.cpp:(.text$_ZN14MyGraphicsViewC1EP14QGraphicsScene[MyGraphicsView::MyGraphicsView(QGraphicsScene*)]+0x27): undefined reference to `vtable for MyGraphicsView'
debug/main.o:main.cpp:(.text$_ZN14MyGraphicsViewC1EP14QGraphicsScene[MyGraphicsView::MyGraphicsView(QGraphicsScene*)]+0x31): undefined reference to `vtable for MyGraphicsView'
debug/main.o:main.cpp:(.text$_ZN14MyGraphicsViewD1Ev[MyGraphicsView::~MyGraphicsView()]+0xb): undefined reference to `vtable for MyGraphicsView'
debug/main.o:main.cpp:(.text$_ZN14MyGraphicsViewD1Ev[MyGraphicsView::~MyGraphicsView()]+0x15): undefined reference to `vtable for MyGraphicsView'
------解决方案--------------------
先把类分成.h和.cpp两个文件。因为有Q_OBJECT宏,需要运行MOC,而MOC要求类的声明和实现分开。
------解决方案--------------------
把类分成.h和.cpp两个文件,因为有Q_OBJECT宏,需要运行MOC,而MOC要求类的声明和实现分开。