PDA

View Full Version : QPainterPath drawing problem



shad
17th January 2007, 09:40
Why does QPainterPath contains so few methods for drawing graphic primitives? How can I draw rounded rect on QPainterPath?

Let me describe what I want - I have some graphic primitives like rounded rect and want to draw them as a background for QGraphicsView, so I decided to draw them once to QPainterPath and draw it to view in QGraphicsView::drawBackground, but seems that QPainterPath miss methods I need (drawRoundRect for example).

wysota
17th January 2007, 11:42
You can make a round rectangle by adding a rectangle and then substracting the corners by adding arcs. Just look at the QPainterPath docs, there is an example there that shows an object with removed interiors. Another possibility is to compose the path from four lines and four arcs and making sure the path is closed.

shad
17th January 2007, 12:38
no, I can't use rect because I need several rects with variable roundedness (from minimum to maximum), I can't "emulate" them with rect or arcs.

camel
17th January 2007, 14:30
Draw them yourself?



const qreal top = XXX;
const qreal left = XXX;
const qreal bottom = XXX;
const qreal right = XXX;

const qreal xMaxRoundness = XXX;
const qreal yMaxRoundness = XXX;

const qreal roundnessSizeX = qMin((qAbs(right - left) / 2.0), xMaxRoundness);
const qreal roundnessSizeY = qMin((qAbs(top - bottom) / 2.0), yMaxRoundness);

QPainterPath roundedRect(QPointF(left, top - roundnessSizeY));
roundedRect.arcTo(left, top,
roundnessSizeX * 2.0, roundnessSizeY * 2.0,
180.0, -90.0);
roundedRect.arcTo(right - roundnessSizeX * 2.0, top,
roundnessSizeX * 2.0, roundnessSizeY * 2.0,
90.0, -90.0);
roundedRect.arcTo(right - roundnessSizeX * 2.0, bottom - roundnessSizeY * 2.0,
roundnessSizeX * 2.0, roundnessSizeY * 2.0,
0.0 , -90.0);
roundedRect.arcTo(leftPosition, bottom - roundnessSizeY * 2.0,
roundnessSizeX * 2.0, roundnessSizeY * 2.0,
-90.0, -90.0);
roundedRect.closeSubpath();