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

Generate an outline from a set of rectangles



  • Hey everyone!

    I'm trying to create an outline path from a set of rectangles using QPainterPath and QPainterPathStroke but I'm not getting the desired result.

    Basically, I'm creating a path by adding rectangles to it by using addRect(), after that, I draw the generated path in my QGraphicsScene. What I need is something like the image below:

    path_outline.png

    As you can see, I need a path that is composed of the extreme points of the rectangles or by joining their bounding rectangles in such a way to produce a polygon that can serve as an outline item without having lines crossing over the rectangles. Unfortunately, the result I'm getting is:

    path_outline1.png

    I'm pretty sure that I'm missing something. So, Is there something that can help me to produce that outline? Any tips? Examples?

    Thanks in advance.


  • Lifetime Qt Champion

    Hi,

    I am pretty sure it won't do what you want but what do you get if you make different path for each rectangle and use QPainterPath::connectPath ?

    Otherwise, you would have to store your rectangle and then add the three points of each that would generate the outline you want.



  • @SGaist Hi! thanks for your reply.

    what do you get if you make different path for each rectangle and use QPainterPath

    I don't get it... could you elaborate, please?

    Otherwise, you would have to store your rectangle and then add the three points of each that would generate the outline you want.

    I've already tried the three points approach, but it didn't work well. Since the arrangement of the rectangles can be changed by the user it's quite complicated to determine which points to use as a reference to create the path.

    Anyway, I'll keep trying to create a solution for this problem.

    Thanks once again!


  • Lifetime Qt Champion

    Sorry, my link text was broken. It's fixed now. The word missing was connectPath.



  • create points and draw them as polylines. N is the number. The first and the last points are the same.

    QPainter painter( this );
    painter.setRenderHint( QPainter::Antialiasing, true );
    painter.setPen( m_pen );
    painter.setBrush( m_brush );
    painter.save();
    
    painter.drawPolyline( m_polylinePoints, N );
    
    painter.restore();
    

    First order your rectangles from top to bottom and left to right by comparing the left top corner coordinates.


Log in to reply