QPainterPath from list of points
-
@Joel-Bodenmann
Sorry for updating a previous post, I didn't realize you were writing at that same time. Look up my suggestion, and if there's something unclear I'll try to expand.Kind regards.
-
@kshegunov
Thanks for your answer. I guess I'll have to figure out how to properly implement this in the next couple of days then. I'll definitely publish results here so people don't have to do that in the future themselves :) -
@Joel-Bodenmann
Also QPainterPathStroker::createStroke coupled with QPainterPath::toFillPolygon might be the easiest way to get what you're after. -
@kshegunov
Can you elaborate? I still need to get the outline of the stroked polygon somehow, no? -
@Joel-Bodenmann
Hello,
Surely, note however that I've not tried this. Set the pen width with your desired offset. Create the stroke, with thecreateStroke
method. The outline of the stroke is supposed to be (as per the documentation) the painter path you get from it. Convert the path to a polygon with thetoFillPolygon
method. If it doesn't work, you can always revert to implementing it yourself, but if it does, well, it looks simpler. :)Kind regards.
-
@kshegunov
Thanks for the explanation. The trouble I am having is getting theQPainterPath
(that needs to be passed toQPainterPathStroker::createStroke()
from my list of lines. The only reasonable option I see isQPainterPath::addPolygon()
but that I can't use as I don't have a polygon, I only have a polyline (like a non-closed polygon).Any thoughts?
-
@Joel-Bodenmann
Hello,
One thing is to create the path from a polygon (no one says it has to be a closed one ;)). You could also perhaps get it by constructing the path from your points withQPainterPath::moveTo
andQPainterPath::lineTo
. The latter would be my choice if I were supposed to compose a painter path.Kind regards.
-
As mentioned above you can use QPainterPathStroker. The problem is it will create tiny loops on the inside of the joints, so you need to simplify the path afterwards.
Sample code:QVector<QPoint> points { QPoint(30,30), QPoint(100, 100), QPoint(200,50) }; QPainterPath basePath; basePath.addPolygon(QPolygon(points)); QPainterPathStroker str; str.setCapStyle(Qt::RoundCap); str.setWidth(10.0); QPainterPath resultPath = str.createStroke(basePath).simplified();
-
Wow, I am deeply impressed. Using the code shown by @Chris-Kawa gave exactly the desired result:
Thank you very much for your help, guys. Very appreciated!
-
well what can we say ?
Chris rules :) -
@mrjj Well, thanks to you and @kshegunov too. You guys are amazing!