I'm trying to create a custom container widget at the moment using QGroupBox as a base and drawing the new frame in the paint event, which is all working fine using drawPolyLine to create it, but I'd like to draw the frame with rounded corners. Has anyone come across a way to do it with drawPolyLine or would I need to rewrite my code to implement them?
Asked
Active
Viewed 3,761 times
3
-
`void QPainter::drawRoundedRect(const QRectF& rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)`? – cmannett85 Jun 12 '12 at 14:17
-
It's not a complete rectangle, imagine a binder divider for the shape. – Nicholas Smith Jun 12 '12 at 14:23
-
As it's irregular, you can create complex shapes from simple ones by 'layering' them on top of each other using `QPainterPath`. – cmannett85 Jun 12 '12 at 14:28
-
You should be able to do this by setting a QPen with the appropriate joinStyle(). – Dan Milburn Jun 12 '12 at 14:31
-
@cbamber85: that's one of the options, it just means swapping QPen's to 'erase' the top section. – Nicholas Smith Jun 12 '12 at 14:37
-
@DanMilburn: I thought that, but it keeps it as a square corner. – Nicholas Smith Jun 12 '12 at 14:39
2 Answers
3
When it comes to custom flexible shapes, QPainterPath is the most powerful class of them all. You could for example use QPainterPath::arcTo() in order to draw single rounded corners, though painting the full shape might require some math.
Another possibility is defining single shapes and merging them using intersected()
or subtracted()
, as already suggested by cbamber85 in the comments.

Tim Meyer
- 12,210
- 8
- 64
- 97
-
Ah, I hadn't seen the intersected method, that might do quite nicely. I was hoping I could get the rounded corners free of charge but I'll just work around it! – Nicholas Smith Jun 12 '12 at 14:41
3
QPen has a "Cap Style" option of Qt::RoundCap which could result in rounded corners depending on pen width.

spider karma
- 87
- 6