qt - Incompatible type in custom QGraphicsPixmapItem -
i have following code returns "assigning 'graphicspixmapitem *' incompatible type 'graphicspixmapitem *' compiler error.
can me?
here code:
the main file:
#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(); } the custom graphicsscene header:
#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 the custom 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); } the custom qgraphicspixmapitem header:
#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 and custom 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); }
qgraphicsscene::addpixmap() returns qgraphicspixmapitem. you're trying assign pointer qgraphicspixmapitem pointer graphicspixmapitem, different types.
also note assigning buf using new, , calling qgraphicsscene::addpixmap(), you're creating 2 distinct objects, namely 1 graphicspixmapitem (from new) , 1 qgraphicspixmap (from addpixmap) item.
what want buf->setpixmap(contactinfo); , call additem(buf); scene constructor, , eliminate addpixmap() call.
Comments
Post a Comment