2013-05-20 37 views
0

我有下面的代碼返回一個「分配給‘GraphicsPixmapItem *’從不兼容型‘GraphicsPixmapItem *’編譯器錯誤。不兼容型定製QGraphicsPixmapItem

有人可以幫我嗎?

下面是代碼:

主要文件:

#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); 
} 

回答

1

QGraphicsScene :: addPixmap()返回一個QGraphicsPixmapItem。您試圖將指向QGraphicsPixmapItem的指針分配給指向GraphicsPixmapItem的指針,這些指針是不同類型的。

還要注意,將其分配給BUF使用新的,然後調用QGraphicsScene::addPixmap(),你要創建兩個不同的對象,即一個GraphicsPixmapItem(從new)和一個QGraphicsPixmap(從addPixmap)項目。

你可能想要的是類似buf->setPixmap(Contactinfo);的東西,然後從你的場景構造函數中調用addItem(buf);,並消除addPixmap()調用。