我有以下代码返回一个“赋值到'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);
}发布于 2013-05-20 10:54:56
QGraphicsScene::addPixmap()返回QGraphicsPixmapItem。您正在尝试将指向QGraphicsPixmapItem的指针赋值给指向GraphicsPixmapItem的指针,这是不同类型的。
还要注意,通过使用new赋值给buf,然后调用QGraphicsScene::addPixmap(),您将创建两个不同的对象,即一个GraphicsPixmapItem (来自new)和一个QGraphicsPixmap (来自addPixmap)。
您可能需要类似于buf->setPixmap(Contactinfo);的内容,然后从场景构造函数中调用addItem(buf);,并消除addPixmap()调用。
https://stackoverflow.com/questions/16641341
复制相似问题