我有以下代码返回“从不兼容类型'GraphicsPixmapItem *'编译器错误分配给'GraphicsPixmapItem *'。
有人可以帮助我吗?
这是代码:
主文件:
#include "graphicsscene.h"
#include <QApplication>
#include <QGraphicsView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GraphicsScene scene;
scene.setSceneRect(0, 0, 318, 458);
QGraphicsView view(&scene);
view.setBackgroundBrush(QPixmap(":/images/background.jpg"));
view.show();
return a.exec();
}
自定义 GraphicsScene 标头:
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include "graphicspixmapitem.h"
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GraphicsScene(QWidget *parent = 0);
QGraphicsPixmapItem *Logo;
};
#endif // GRAPHICSSCENE_H
自定义 GraphicsScene cpp:
#include "graphicsscene.h"
GraphicsScene::GraphicsScene(QWidget *parent) :
QGraphicsScene()
{
QPixmap Contactinfo(":/images/ScreenContacts.png");
GraphicsPixmapItem *buf = new GraphicsPixmapItem;
buf = addPixmap(Contactinfo);
buf->setPos(0, 40);
buf->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsScenePositionChanges);
}
自定义 QGraphicsPixmapItem 标头:
#ifndef GRAPHICSPIXMAPITEM_H
#define GRAPHICSPIXMAPITEM_H
#include <QObject>
#include <QGraphicsPixmapItem>
class GraphicsPixmapItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
GraphicsPixmapItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
};
#endif // GRAPHICSPIXMAPITEM_H
最后是自定义 QGraphicsPixmapItem cpp:
#include "graphicspixmapitem.h"
GraphicsPixmapItem::GraphicsPixmapItem(QGraphicsItem *parent, QGraphicsScene *scene)
: QGraphicsPixmapItem(parent, scene)
{
}
#include <QDebug>
QVariant GraphicsPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
qDebug() << "itemChange Triggered";
if (change == ItemPositionChange) {
qDebug() << "Position changed";
}
return QGraphicsItem::itemChange(change, value);
}