Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickrectangleshape.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
8
9void QQuickRectangleShapePrivate::updateStrokeAdjustment()
10{
11 Q_Q(QQuickRectangleShape);
12 switch (borderMode) {
13 case QQuickRectangleShape::Inside:
14 pathRectangle->setStrokeAdjustment(q->strokeWidth());
15 break;
16 case QQuickRectangleShape::Middle:
17 pathRectangle->setStrokeAdjustment(0);
18 break;
19 case QQuickRectangleShape::Outside:
20 pathRectangle->setStrokeAdjustment(-q->strokeWidth());
21 break;
22 }
23 q->polish();
24}
25
26QQuickRectangleShape::QQuickRectangleShape(QQuickItem *parent)
27 : QQuickShape(*(new QQuickRectangleShapePrivate), parent)
28{
29 Q_D(QQuickRectangleShape);
30
31 setPreferredRendererType(CurveRenderer);
32
33 d->shapePath = new QQuickShapePath(this);
34 d->shapePath->setObjectName("rectangleShapeShapePath");
35 d->shapePath->setAsynchronous(true);
36 d->shapePath->setStrokeWidth(4);
37 d->shapePath->setStrokeColor(QColorConstants::Black);
38
39 connect(d->shapePath, &QQuickShapePath::strokeColorChanged, this, &QQuickRectangleShape::strokeColorChanged);
40 connect(d->shapePath, &QQuickShapePath::strokeWidthChanged, this, &QQuickRectangleShape::strokeWidthChanged);
41 connect(d->shapePath, &QQuickShapePath::fillColorChanged, this, &QQuickRectangleShape::fillColorChanged);
42 connect(d->shapePath, &QQuickShapePath::joinStyleChanged, this, &QQuickRectangleShape::joinStyleChanged);
43 connect(d->shapePath, &QQuickShapePath::capStyleChanged, this, &QQuickRectangleShape::capStyleChanged);
44 connect(d->shapePath, &QQuickShapePath::strokeStyleChanged, this, &QQuickRectangleShape::strokeStyleChanged);
45 connect(d->shapePath, &QQuickShapePath::dashOffsetChanged, this, &QQuickRectangleShape::dashOffsetChanged);
46 connect(d->shapePath, &QQuickShapePath::dashPatternChanged, this, &QQuickRectangleShape::dashPatternChanged);
47 // QQuickShapePath has no change signal for fillGradient.
48
49 // Construct and append the PathRectangle to the ShapePath.
50 d->pathRectangle = new QQuickPathRectangle(d->shapePath);
51 d->pathRectangle->setObjectName("topRightPathArc");
52 d->pathRectangle->setRadius(10);
53 connect(d->pathRectangle, &QQuickPathRectangle::radiusChanged, this, &QQuickRectangleShape::radiusChanged);
54 connect(d->pathRectangle, &QQuickPathRectangle::topLeftRadiusChanged, this, &QQuickRectangleShape::topLeftRadiusChanged);
55 connect(d->pathRectangle, &QQuickPathRectangle::topRightRadiusChanged, this, &QQuickRectangleShape::topRightRadiusChanged);
56 connect(d->pathRectangle, &QQuickPathRectangle::bottomLeftRadiusChanged, this, &QQuickRectangleShape::bottomLeftRadiusChanged);
57 connect(d->pathRectangle, &QQuickPathRectangle::bottomRightRadiusChanged, this, &QQuickRectangleShape::bottomRightRadiusChanged);
58 connect(d->pathRectangle, &QQuickPathRectangle::bevelChanged, this, &QQuickRectangleShape::bevelChanged);
59 connect(d->pathRectangle, &QQuickPathRectangle::topLeftBevelChanged, this, &QQuickRectangleShape::topLeftBevelChanged);
60 connect(d->pathRectangle, &QQuickPathRectangle::topRightBevelChanged, this, &QQuickRectangleShape::topRightBevelChanged);
61 connect(d->pathRectangle, &QQuickPathRectangle::bottomLeftBevelChanged, this, &QQuickRectangleShape::bottomLeftBevelChanged);
62 connect(d->pathRectangle, &QQuickPathRectangle::bottomRightBevelChanged, this, &QQuickRectangleShape::bottomRightBevelChanged);
63
64 // geometryChange sets PathRectangle's size to ours, so do this after we've created it.
65 setWidth(200);
66 setHeight(150);
67 d->updateStrokeAdjustment();
68
69 QQuickPathPrivate::get(d->shapePath)->appendPathElement(d->pathRectangle);
70
71 // Now that we've constructed the individual path elements and added them to the shape path,
72 // add that path as a child of us.
73 // Do what vpe_append in qquickshape.cpp does except without the overhead of the QQmlListProperty stuff.
74 d->sp.append(d->shapePath);
75 // Similar, but for QQuickItemPrivate::data_append...
76 d->shapePath->setParent(this);
77 // ... which calls QQuickItemPrivate::resources_append.
78 d->extra.value().resourcesList.append(d->shapePath);
79}
80
81/*!
82 \qmltype RectangleShape
83 \inqmlmodule QtQuick.Shapes.DesignHelpers
84 \brief A filled rectangle with an optional border.
85 \since QtQuick 6.10
86
87 RectangleShape is used to fill areas with solid color or gradients and to
88 provide a rectangular border.
89
90 Each Rectangle item is painted using either a solid fill color, specified
91 using the \l fillColor property, or a gradient, defined using one of the \l
92 ShapeGradient subtypes and set using the \l gradient property. If both a
93 color and a gradient are specified, the gradient is used.
94
95 An optional border can be added to a rectangle with its own color and
96 thickness by setting the \l strokeColor and \l strokeWidth properties.
97 Setting the color to \c transparent creates a border without a fill color.
98
99 Rounded rectangles can be drawn using the \l radius property. The radius
100 can also be specified separately for each corner. Additionally, \l bevel
101 can be applied on any corner to cut it off sharply.
102
103 RectangleShape's default value for \l preferredRendererType is
104 \c Shape.CurveRenderer.
105
106 \section1 Example Usage
107
108 \snippet rectangleshape-bevel.qml rectangleShape
109
110 \image pathrectangle-bevel.png
111*/
112
113QQuickRectangleShape::~QQuickRectangleShape()
114{
115}
116
117/*!
118 \include pathrectangle.qdocinc {radius-property}
119 {QtQuick.Shapes.DesignHelpers::RectangleShape}
120
121 The default value is \c 10.
122*/
123
124int QQuickRectangleShape::radius() const
125{
126 Q_D(const QQuickRectangleShape);
127 return d->pathRectangle->radius();
128}
129
130void QQuickRectangleShape::setRadius(int radius)
131{
132 Q_D(QQuickRectangleShape);
133 d->pathRectangle->setRadius(radius);
134}
135
136/*!
137 \include pathrectangle.qdocinc {radius-properties}
138 {QtQuick.Shapes.DesignHelpers::RectangleShape} {rectangleshape.qml} {rectangleShape}
139*/
140
141int QQuickRectangleShape::topLeftRadius() const
142{
143 Q_D(const QQuickRectangleShape);
144 return d->pathRectangle->topLeftRadius();
145}
146
147void QQuickRectangleShape::setTopLeftRadius(int topLeftRadius)
148{
149 Q_D(QQuickRectangleShape);
150 d->pathRectangle->setTopLeftRadius(topLeftRadius);
151}
152
153void QQuickRectangleShape::resetTopLeftRadius()
154{
155 Q_D(QQuickRectangleShape);
156 d->pathRectangle->resetTopLeftRadius();
157}
158
159int QQuickRectangleShape::topRightRadius() const
160{
161 Q_D(const QQuickRectangleShape);
162 return d->pathRectangle->topRightRadius();
163}
164
165void QQuickRectangleShape::setTopRightRadius(int topRightRadius)
166{
167 Q_D(QQuickRectangleShape);
168 d->pathRectangle->setTopRightRadius(topRightRadius);
169}
170
171void QQuickRectangleShape::resetTopRightRadius()
172{
173 Q_D(QQuickRectangleShape);
174 d->pathRectangle->resetTopRightRadius();
175}
176
177int QQuickRectangleShape::bottomLeftRadius() const
178{
179 Q_D(const QQuickRectangleShape);
180 return d->pathRectangle->bottomLeftRadius();
181}
182
183void QQuickRectangleShape::setBottomLeftRadius(int bottomLeftRadius)
184{
185 Q_D(QQuickRectangleShape);
186 d->pathRectangle->setBottomLeftRadius(bottomLeftRadius);
187}
188
189void QQuickRectangleShape::resetBottomLeftRadius()
190{
191 Q_D(QQuickRectangleShape);
192 d->pathRectangle->resetBottomLeftRadius();
193}
194
195int QQuickRectangleShape::bottomRightRadius() const
196{
197 Q_D(const QQuickRectangleShape);
198 return d->pathRectangle->bottomRightRadius();
199}
200
201void QQuickRectangleShape::setBottomRightRadius(int bottomRightRadius)
202{
203 Q_D(QQuickRectangleShape);
204 d->pathRectangle->setBottomRightRadius(bottomRightRadius);
205}
206
207void QQuickRectangleShape::resetBottomRightRadius()
208{
209 Q_D(QQuickRectangleShape);
210 d->pathRectangle->resetBottomRightRadius();
211}
212
213/*!
214 \include pathrectangle.qdocinc {bevel-property}
215 {QtQuick.Shapes.DesignHelpers::RectangleShape}
216 {rectangleshape-bevel.qml}{rectangleShape}
217*/
218
219bool QQuickRectangleShape::hasBevel() const
220{
221 Q_D(const QQuickRectangleShape);
222 return d->pathRectangle->hasBevel();
223}
224
225void QQuickRectangleShape::setBevel(bool bevel)
226{
227 Q_D(QQuickRectangleShape);
228 d->pathRectangle->setBevel(bevel);
229}
230
231/*!
232 \include pathrectangle.qdocinc {bevel-properties}
233 {QtQuick.Shapes.DesignHelpers::RectangleShape}
234 {rectangleshape.qml} {rectangleShape}
235*/
236
237bool QQuickRectangleShape::hasTopLeftBevel() const
238{
239 Q_D(const QQuickRectangleShape);
240 return d->pathRectangle->hasTopLeftBevel();
241}
242
243void QQuickRectangleShape::setTopLeftBevel(bool topLeftBevel)
244{
245 Q_D(QQuickRectangleShape);
246 d->pathRectangle->setTopLeftBevel(topLeftBevel);
247}
248
249void QQuickRectangleShape::resetTopLeftBevel()
250{
251 Q_D(QQuickRectangleShape);
252 d->pathRectangle->resetTopLeftBevel();
253}
254
255bool QQuickRectangleShape::hasTopRightBevel() const
256{
257 Q_D(const QQuickRectangleShape);
258 return d->pathRectangle->hasTopRightBevel();
259}
260
261void QQuickRectangleShape::setTopRightBevel(bool topRightBevel)
262{
263 Q_D(QQuickRectangleShape);
264 d->pathRectangle->setTopRightBevel(topRightBevel);
265}
266
267void QQuickRectangleShape::resetTopRightBevel()
268{
269 Q_D(QQuickRectangleShape);
270 d->pathRectangle->resetTopRightBevel();
271}
272
273bool QQuickRectangleShape::hasBottomLeftBevel() const
274{
275 Q_D(const QQuickRectangleShape);
276 return d->pathRectangle->hasBottomLeftBevel();
277}
278
279void QQuickRectangleShape::setBottomLeftBevel(bool bottomLeftBevel)
280{
281 Q_D(QQuickRectangleShape);
282 d->pathRectangle->setBottomLeftBevel(bottomLeftBevel);
283}
284
285void QQuickRectangleShape::resetBottomLeftBevel()
286{
287 Q_D(QQuickRectangleShape);
288 d->pathRectangle->resetBottomLeftBevel();
289}
290
291bool QQuickRectangleShape::hasBottomRightBevel() const
292{
293 Q_D(const QQuickRectangleShape);
294 return d->pathRectangle->hasBottomRightBevel();
295}
296
297void QQuickRectangleShape::setBottomRightBevel(bool bottomRightBevel)
298{
299 Q_D(QQuickRectangleShape);
300 d->pathRectangle->setBottomRightBevel(bottomRightBevel);
301}
302
303void QQuickRectangleShape::resetBottomRightBevel()
304{
305 Q_D(QQuickRectangleShape);
306 d->pathRectangle->resetBottomRightBevel();
307}
308
309/*!
310 \qmlproperty color QtQuick.Shapes.DesignHelpers::RectangleShape::strokeColor
311
312 This property holds the stroking color.
313
314 When set to \c transparent, no stroking occurs.
315
316 The default value is \c "black".
317*/
318
319QColor QQuickRectangleShape::strokeColor() const
320{
321 Q_D(const QQuickRectangleShape);
322 return d->shapePath->strokeColor();
323}
324
325void QQuickRectangleShape::setStrokeColor(const QColor &color)
326{
327 Q_D(QQuickRectangleShape);
328 d->shapePath->setStrokeColor(color);
329}
330
331/*!
332 \qmlproperty real QtQuick.Shapes.DesignHelpers::RectangleShape::strokeWidth
333
334 This property holds the stroke width.
335
336 When set to a negative value, no stroking occurs.
337
338 The default value is \c 1.
339*/
340
341qreal QQuickRectangleShape::strokeWidth() const
342{
343 Q_D(const QQuickRectangleShape);
344 return d->shapePath->strokeWidth();
345}
346
347void QQuickRectangleShape::setStrokeWidth(qreal width)
348{
349 Q_D(QQuickRectangleShape);
350 const qreal oldStrokeWidth = d->shapePath->strokeWidth();
351 d->shapePath->setStrokeWidth(width);
352 // setStrokeWidth doesn't use qFuzzyCompare, so neither do we.
353 if (oldStrokeWidth != d->shapePath->strokeWidth())
354 d->updateStrokeAdjustment();
355}
356
357/*!
358 \qmlproperty color QtQuick.Shapes.DesignHelpers::RectangleShape::fillColor
359
360 This property holds the fill color.
361
362 When set to \c transparent, no filling occurs.
363
364 The default value is \c "white".
365
366 \note If either \l fillGradient is set to something other than \c null, it
367 will be used instead of \c fillColor.
368*/
369
370QColor QQuickRectangleShape::fillColor() const
371{
372 Q_D(const QQuickRectangleShape);
373 return d->shapePath->fillColor();
374}
375
376void QQuickRectangleShape::setFillColor(const QColor &color)
377{
378 Q_D(QQuickRectangleShape);
379 d->shapePath->setFillColor(color);
380}
381
382/*!
383 \include shapepath.qdocinc {fillRule-property} {QtQuick.Shapes.DesignHelpers::RectangleShape}
384*/
385
386QQuickShapePath::FillRule QQuickRectangleShape::fillRule() const
387{
388 Q_D(const QQuickRectangleShape);
389 return d->shapePath->fillRule();
390}
391
392void QQuickRectangleShape::setFillRule(QQuickShapePath::FillRule fillRule)
393{
394 Q_D(QQuickRectangleShape);
395 d->shapePath->setFillRule(fillRule);
396}
397
398/*!
399 \include shapepath.qdocinc {joinStyle-property} {QtQuick.Shapes.DesignHelpers::RectangleShape}
400*/
401
402QQuickShapePath::JoinStyle QQuickRectangleShape::joinStyle() const
403{
404 Q_D(const QQuickRectangleShape);
405 return d->shapePath->joinStyle();
406}
407
408void QQuickRectangleShape::setJoinStyle(QQuickShapePath::JoinStyle style)
409{
410 Q_D(QQuickRectangleShape);
411 d->shapePath->setJoinStyle(style);
412}
413
414/*!
415 \include shapepath.qdocinc {capStyle-property} {QtQuick.Shapes.DesignHelpers::RectangleShape}
416*/
417
418QQuickShapePath::CapStyle QQuickRectangleShape::capStyle() const
419{
420 Q_D(const QQuickRectangleShape);
421 return d->shapePath->capStyle();
422}
423
424void QQuickRectangleShape::setCapStyle(QQuickShapePath::CapStyle style)
425{
426 Q_D(QQuickRectangleShape);
427 d->shapePath->setCapStyle(style);
428}
429
430/*!
431 \include shapepath.qdocinc {strokeStyle-property} {QtQuick.Shapes.DesignHelpers::RectangleShape}
432*/
433
434QQuickShapePath::StrokeStyle QQuickRectangleShape::strokeStyle() const
435{
436 Q_D(const QQuickRectangleShape);
437 return d->shapePath->strokeStyle();
438}
439
440void QQuickRectangleShape::setStrokeStyle(QQuickShapePath::StrokeStyle style)
441{
442 Q_D(QQuickRectangleShape);
443 d->shapePath->setStrokeStyle(style);
444}
445
446/*!
447 \include shapepath.qdocinc {dashOffset-property} {QtQuick.Shapes.DesignHelpers::RectangleShape}
448*/
449
450qreal QQuickRectangleShape::dashOffset() const
451{
452 Q_D(const QQuickRectangleShape);
453 return d->shapePath->dashOffset();
454}
455
456void QQuickRectangleShape::setDashOffset(qreal offset)
457{
458 Q_D(QQuickRectangleShape);
459 d->shapePath->setDashOffset(offset);
460}
461
462/*!
463 \include shapepath.qdocinc {dashPattern-property} {QtQuick.Shapes.DesignHelpers::RectangleShape}
464*/
465
466QVector<qreal> QQuickRectangleShape::dashPattern() const
467{
468 Q_D(const QQuickRectangleShape);
469 return d->shapePath->dashPattern();
470}
471
472void QQuickRectangleShape::setDashPattern(const QVector<qreal> &array)
473{
474 Q_D(QQuickRectangleShape);
475 d->shapePath->setDashPattern(array);
476}
477
478/*!
479 \qmlproperty ShapeGradient QtQuick.Shapes.DesignHelpers::RectangleShape::fillGradient
480
481 The fillGradient of the rectangle fill color.
482
483 By default, no fillGradient is enabled and the value is null. In this case, the
484 fill uses a solid color based on the value of \l fillColor.
485
486 When set, \l fillColor is ignored and filling is done using one of the
487 \l ShapeGradient subtypes.
488
489 \note The \l Gradient type cannot be used here. Rather, prefer using one of
490 the advanced subtypes, like \l LinearGradient.
491*/
492QQuickShapeGradient *QQuickRectangleShape::fillGradient() const
493{
494 Q_D(const QQuickRectangleShape);
495 return d->shapePath->fillGradient();
496}
497
498void QQuickRectangleShape::setFillGradient(QQuickShapeGradient *fillGradient)
499{
500 Q_D(QQuickRectangleShape);
501 d->shapePath->setFillGradient(fillGradient);
502}
503
504void QQuickRectangleShape::resetFillGradient()
505{
506 setFillGradient(nullptr);
507}
508
509/*!
510 \qmlproperty enumeration QtQuick.Shapes.DesignHelpers::RectangleShape::borderMode
511
512 The \l borderMode property determines where the border is drawn along the
513 edge of the rectangle.
514
515 \value RectangleShape.Inside
516 The border is drawn along the inside edge of the item and does not
517 affect the item width.
518
519 This is the default value.
520 \value RectangleShape.Middle
521 The border is drawn over the edge of the item and does not
522 affect the item width.
523 \value RectangleShape.Outside
524 The border is drawn along the outside edge of the item and increases
525 the item width by the value of \l strokeWidth.
526
527 \sa strokeWidth
528*/
529QQuickRectangleShape::BorderMode QQuickRectangleShape::borderMode() const
530{
531 Q_D(const QQuickRectangleShape);
532 return d->borderMode;
533}
534
535void QQuickRectangleShape::setBorderMode(BorderMode borderMode)
536{
537 Q_D(QQuickRectangleShape);
538 if (borderMode == d->borderMode)
539 return;
540
541 d->borderMode = borderMode;
542 d->updateStrokeAdjustment();
543 emit borderModeChanged();
544}
545
546void QQuickRectangleShape::resetBorderMode()
547{
548 setBorderMode(Inside);
549}
550
551void QQuickRectangleShape::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
552{
553 Q_D(QQuickRectangleShape);
554 QQuickShape::geometryChange(newGeometry, oldGeometry);
555 d->pathRectangle->setWidth(newGeometry.width());
556 d->pathRectangle->setHeight(newGeometry.height());
557}
558
559QT_END_NAMESPACE
560
561#include "moc_qquickrectangleshape_p.cpp"
Combined button and popup list for selecting options.