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

QPolygonF for non-intersecting subpaths



  • Hi,
    I am trying to make a QPolygonF from two non-intersecting subpaths. I added two paths but when I used united to group them they still connected.

    QPainterPath path1;
            qreal Width =100;
            qreal Height = 50;
            path1.moveTo(QPointF(X,Y));
            path1.lineTo(QPointF(X + Width, Y ));
            path1.lineTo(QPointF(X + Width, Y + Height));
            path1.lineTo(QPointF(X , Y + Height));
            path1.closeSubpath();
    
            QPainterPath path2;
            path2.moveTo(QPointF(X + 1.2*Width, Y + 1.2*Height));
            path2.addRoundedRect(QRectF(X + 1.2*Width, Y + 1.2*Height,Width,Height),0.1*Width, 0.1*Height,Qt::AbsoluteSize);
    
            QPolygonF po1 = path1.toSubpathPolygons()[0];
            QPolygonF po2 = path2.toSubpathPolygons()[0];
    
            myPolygon = po2.united(po1);
    

    this is what I get
    Capture.JPG

    As I understand toSubpathPolygons() and/or united should be used but I don't know how. Any idea, please.



  • @sDmt

    What kind of polygon do you expect? How it should look like?



  • @Pl45m4 thanks for the reply.
    I just need to get rid of the line connecting the two shapes. Also, I want to handle them as group such that when I drag one the other is dragged as well ans so on.



  • @sDmt

    It seems that united (adding two separate paths) connects the path1' s end point with the starting point of path2... This is the line, you can see there.

    So I guess you cant use PainterPath to draw an item like this. Use a custom QGraphicsItem instead, where you add both rectangles or polygons to your painter and set a boundingRect that includes both of them.

    QRectF MyItem::boundingRect() const
    {
        return QRectF(-10, -10, 30, 30);
    }
    
    
    void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
            painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
            painter->drawRoundedRect(10, 10, 5, 5, 5, 5);
    }
    


  • @Pl45m4 True, I see that QPainterPath is getting me nowhere in this case. Thanks.