How to use a QPixmap as an object in DiagramScene?
Unsolved
General and Desktop
-
I'm kind of adapting the DiagramScene example(it's on qt the site)
But in the project it uses a QPolygon as an object, but i want to use a Pixmap instead.
It's a program composed by some classes:
diagramitem is responsible fot the items that we will place on the screen
diagramscene handles the act of placing an object on the screenI thought that it is i little bit confusing the that qt did it, but ok
diagramitem.cpp
DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu, QGraphicsItem *parent) : QGraphicsPolygonItem(parent) { QPainter *painter=new QPainter; myDiagramType = diagramType; myContextMenu = contextMenu; QPainterPath path; switch (myDiagramType) { case StartEnd: path.moveTo(200, 50); path.arcTo(150, 0, 50, 50, 0, 90); path.arcTo(50, 0, 50, 50, 90, 90); path.arcTo(50, 50, 50, 50, 180, 90); path.arcTo(150, 50, 50, 50, 270, 90); path.lineTo(200, 25); myPolygon = path.toFillPolygon(); break; case Conditional: path.addEllipse(QPoint(0,0),50,50); path.addEllipse(QPoint(50,0),50,50); myPolygon=path.toFillPolygon(); break; case Step: myPolygon << QPointF(-100, -100) << QPointF(100, -100) << QPointF(100, 100) << QPointF(-100, 100) << QPointF(-100, -100); break; default: myPolygon << QPointF(-120, -80) << QPointF(-70, 80) << QPointF(120, 80) << QPointF(70, -80) << QPointF(-120, -80); break; } setPolygon(myPolygon); setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); } void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { scene()->clearSelection(); setSelected(true); myContextMenu->exec(event->screenPos()); } //i didn't understand what this function means
diagramscene.cpp
DiagramScene::DiagramScene(QMenu *itemMenu, QObject *parent) : QGraphicsScene(parent) { myItemMenu = itemMenu; myMode = MoveItem; myItemType = DiagramItem::Step; line = 0; textItem = 0; myItemColor = Qt::white; myTextColor = Qt::black; myLineColor = Qt::black; } void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) { if (mouseEvent->button() != Qt::LeftButton) return; DiagramItem *item; switch (myMode) { case InsertItem: item = new DiagramItem(myItemType, myItemMenu); item->setBrush(myItemColor); addItem(item); item->setPos(mouseEvent->scenePos()); emit itemInserted(item); break; ... }
how can i create a myitemtype object with a pixmap instead of a polygon?
-
Hi,
In that case, why not use a QGraphicsPixmapItem ?
-
I did this but i don't know if i'm right
diagramitem.h
class DiagramItem : public QGraphicsPixmapItem //now its from QGraphicsPixmapItem { public: .... QPixmap polygon() const { return myPolygon; } .... private: DiagramType myDiagramType; QPixmap myPolygon; QMenu *myContextMenu; QList<Line *> arrows; };
now i'm using a pixmap as "myPolygon" instead of using QPolygonF
diagramitem.cpp
DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu, QGraphicsItem *parent) : QGraphicsPixmapItem(parent) { myDiagramType = diagramType; myContextMenu = contextMenu; QPainterPath path; switch (myDiagramType) { case StartEnd:{ QPixmap pixmap("C:/Users/ILHA4/Desktop/diagrama_unifilar/trafo"); myPolygon = pixmap; break; } case Conditional:{ QPixmap pixmap2("C:/Users/ILHA4/Desktop/diagrama_unifilar/gerador"); myPolygon=pixmap2; break; } case Step:{ QPixmap pixmap3("C:/Users/ILHA4/Desktop/diagrama_unifilar/barramento"); myPolygon=pixmap3; break; } default:{ QPixmap pixmap4("C:/Users/ILHA4/Desktop/diagrama_unifilar/carga"); myPolygon=pixmap4; break; } } setPixmap(myPolygon); setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); }
that's it, am i doing something wrong?