又出错了。
。
main.obj:-1: error: LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __thiscall MyView::metaObject(void)const " (?metaObject@MyView@@UBEPBUQMetaObject@@XZ)
main.obj:-1: error: LNK2001: 无法解析的外部符号 "public: virtual void * __thiscall MyView::qt_metacast(char const *)" (?
qt_metacast@MyView@@UAEPAXPBD@Z)
main.obj:-1: error: LNK2001: 无法解析的外部符号 "public: virtual int __thiscall MyView::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyView@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
debugmyScene.exe:-1: error: LNK1120: 3 个无法解析的外部命令
这些错都是什么啊。。
我建立了一个继承自QWidget的类,基类是 QGraphicsView
然后生成了myview.h 自己主动加上了宏Q_OBJECT 所以就报那些错了。
。 原理不懂呀
改完之后,记得清理项目!
myview.h
#ifndef MYVIEW_H
#define MYVIEW_H
#include <QGraphicsView>
class MyView : public QGraphicsView
{
// Q_OBJECT 仅仅需把这里凝视掉即可了
public:
explicit MyView(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *event);
signals:
public slots:
};
#endif // MYVIEW_H
myview.cpp
#include "myview.h"
#include <QMouseEvent>
#include <QGraphicsItem>
#include <QDebug>
MyView::MyView(QWidget *parent) :
QGraphicsView(parent)
{
}
void MyView::mousePressEvent(QMouseEvent *event)
{
QPoint viewPos = event->pos();
qDebug() <<"viewPos:" << viewPos;
QPointF scenePos = mapToScene(viewPos);
qDebug() << "scenePos:" << scenePos;
QTransform transform;
QGraphicsItem *item = scene()->itemAt(scenePos,transform);
if(item)
{
QPointF itemPos = item->mapFromScene(scenePos);
qDebug() << "itemPos:" << itemPos;
}
}
main.cpp
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsItem>
#include <QDebug>
#include <QGraphicsView>
#include "myitem.h"
#include "myitem.cpp"
#include "myview.h"
#include "myview.cpp"
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QGraphicsScene scene;
// QGraphicsRectItem * item = new QGraphicsRectItem(0,0,100,100);
MyItem *item = new MyItem;
scene.addItem(item);
item->setPos(10,10);
QGraphicsRectItem *rectItem = scene.addRect(QRect(0,0,100,100),
QPen(Qt::blue),QBrush(Qt::green));
rectItem->setPos(20,20);
MyView view;
view.setScene(&scene);
view.setForegroundBrush(QColor(255,255,255,100));
view.setBackgroundBrush(QPixmap("../myScene/11.jpg"));
view.show();
return app.exec();
}