天天看点

QT 继承自QGraphicsItem

main.obj:-1: error: LNK2001: 无法解析的外部符号 "public: __thiscall MyItem::MyItem(void)" (??0MyItem@@[email protected])

经过网上的遍历,找到N种解决办法也没解决掉,不过最后终于被我解决掉啦。。哈哈

在mian.cpp中 把myitem.cpp的头文件加上 然后清理项目 在运行就行了

myitem.h

#ifndef MYITEM_H
#define MYITEM_H

class MyItem : public QGraphicsItem
{
public:
    MyItem();
    QRectF boundingRect() const;
    void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
               QWidget *widget);
};

#endif // MYITEM_H
           

main.cpp

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsItem>
#include <QDebug>
#include <QGraphicsView>
#include "myitem.h"
//#include "myitem.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);

    QTransform transform;
    qDebug()<<scene.itemAt(50,50,transform);

    QGraphicsView view(&scene);
    scene.setForegroundBrush(QColor(255,255,255,100));
    scene.setBackgroundBrush(QPixmap("../myScene/11.jpg"));
    view.resize(400,300);
    view.show();
    QGraphicsView view2(&scene);
    view2.resize(400,300);
    view2.show();

    return app.exec();
}
           

myitem.cpp

#include "myitem.h"
#include <QPainter>
#include <QRectF>
MyItem::MyItem()
{
}
QRectF MyItem::boundingRect() const
{
    qreal penWidth = 1;
    return QRectF(0 - penWidth / 2,0 - penWidth / 2,
                  20 + penWidth,20 + penWidth);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
    painter->setBrush(Qt::red);
    painter->drawRect(0,0,20,20);
}
           
Qt

继续阅读