Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Diagram Scene Example ::paint and ::setPolygon



  • as the example Diagram Scene Example:
    diagramitem.cpp

            setPolygon(myPolygon);
            setFlag(QGraphicsItem::ItemIsMovable, true);
            setFlag(QGraphicsItem::ItemIsSelectable, true);
            setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    

    item can select, move;
    but when i change it a little:
    to:

    //setPolygon(myPolygon);
               setFlag(QGraphicsItem::ItemIsMovable, true);
               setFlag(QGraphicsItem::ItemIsSelectable, true);
               setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    
    QRectF  DiagramItem::boundingRect() const
    {
        qreal penWidth = 1;
        switch (myDiagramType) {
            case StartEnd:
                return QRectF(0 - penWidth / 2,0 - penWidth / 2,
                   200 + penWidth, 200 + penWidth);
                break;
            case Conditional:
                return QRectF(-100 - penWidth / 2, -100 - penWidth / 2,
                               200 + penWidth, 200 + penWidth);
            case Step:
                return QRectF(-100 - penWidth / 2, -100 - penWidth / 2,
                           200 + penWidth, 200 + penWidth);
            case Io:
                return QRectF(-120 - penWidth / 2, -80 - penWidth / 2,
                       240 + penWidth, 160 + penWidth);
        }
    
    }
    
    void  DiagramItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
    {
        painter->drawPolygon(myPolygon );
    }
    

    item can show but can be selected and moved, Arrow can not work also;



  • 11e40ad0-85a2-435a-b859-1e9f62171888-image.png



  • Reimplement mouseMoveEvent and mousePressEvent; set breakPiont . finded that it is not executed.



  • This post is deleted!


  • ha, diagramitem Inherit QGraphicsPolygonItem , change it to inherit QGraphicsItem.


Log in to reply