Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use a QPixmap as an object in DiagramScene?

How to use a QPixmap as an object in DiagramScene?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 329 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    frnklu20
    wrote on last edited by frnklu20
    #1

    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 screen

    I 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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      In that case, why not use a QGraphicsPixmapItem ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • F Offline
        F Offline
        frnklu20
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved