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
qquickflexboxlayout.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// Qt-Security score:significant reason:default
4
5#include <QtQuickLayouts/private/qquickflexboxlayout_p.h>
6#include <QtQuickLayouts/private/qquickflexboxlayoutengine_p.h>
7#include <QtQml/qqmlinfo.h>
8
9/*!
10 \qmltype FlexboxLayout
11 //! \nativetype QQuickFlexboxLayout
12 \inherits Item
13 \inqmlmodule QtQuick.Layouts
14 \ingroup layouts
15 \since 6.10
16 \preliminary
17 \brief The FlexboxLayout QML construct provides a flex layout for the
18 quick items.
19
20 The FlexboxLayout enables to layout the quick items in a flexible way as
21 similar to that of
22 \l {https://www.w3.org/TR/css-flexbox-1/}{CSS Flexible Box Layout}.
23 Internally Qt FlexboxLayout uses the \l{qtquick-attribution-yoga.html}{yoga engine}
24 to derive the geometry of
25 the flex items. The \l {https://www.yogalayout.dev/}{yoga library} is a
26 subset of the
27 \l {https://www.w3.org/TR/css-flexbox-1/}{CSS Flexible Box Layout}. Thus
28 FlexboxLayout can be limited to the feature as supported in the
29 \l {https://www.yogalayout.dev/}{yoga library}.
30
31 \note The FlexboxLayout adheres to yoga library version 2.0 for its
32 features.
33
34 The items within the FlexboxLayout can be configured with preferred,
35 minimum and maximum sizes through the existing layout attached properties.
36 For instance, if the item within the FlexboxLayout need to be stretched,
37 the layout attached property \l{Layout::fillWidth}{Layout.fillWidth} or
38 \l{Layout::fillHeight}{Layout.fillHeight} can be set.
39
40 Items in a FlexboxLayout support these attached properties:
41 \list
42 \li \l{FlexboxLayout::alignSelf}{FlexboxLayout.alignSelf}
43 \li \l{Layout::minimumWidth}{Layout.minimumWidth}
44 \li \l{Layout::minimumHeight}{Layout.minimumHeight}
45 \li \l{Layout::preferredWidth}{Layout.preferredWidth}
46 \li \l{Layout::preferredHeight}{Layout.preferredHeight}
47 \li \l{Layout::maximumWidth}{Layout.maximumWidth}
48 \li \l{Layout::maximumHeight}{Layout.maximumHeight}
49 \li \l{Layout::fillWidth}{Layout.fillWidth}
50 \li \l{Layout::fillHeight}{Layout.fillHeight}
51 \li \l{Layout::margins}{Layout.margins}
52 \li \l{Layout::leftMargin}{Layout.leftMargin}
53 \li \l{Layout::rightMargin}{Layout.rightMargin}
54 \li \l{Layout::topMargin}{Layout.topMargin}
55 \li \l{Layout::bottomMargin}{Layout.bottomMargin}
56 \endlist
57
58 Read more about attached properties \l{QML Object Attributes}{here}.
59 \sa ColumnLayout
60 \sa GridLayout
61 \sa RowLayout
62 \sa {QtQuick.Controls::StackView}{StackView}
63 \sa {Qt Quick Layouts Overview}
64
65 To be able to use this type more efficiently, it is recommended that you
66 understand the general mechanism of the Qt Quick Layouts module. Refer to
67 \l{Qt Quick Layouts Overview} for more information.
68
69 \section1 Example Usage
70
71 The following snippet shows the minimalistic example of using QML
72 FlexboxLayout to arrange the rectangle items in more flexible way
73
74 \snippet layouts/simpleFlexboxLayout.qml layout-definition
75
76 \note This API is considered tech preview and may change or be removed in
77 future versions of Qt.
78*/
79
80QT_BEGIN_NAMESPACE
81
82class QQuickFlexboxLayoutPrivate : public QQuickLayoutPrivate
83{
84 Q_DECLARE_PUBLIC(QQuickFlexboxLayout)
85
86public:
87 QQuickFlexboxLayoutPrivate() : QQuickLayoutPrivate() {}
88 const QQuickFlexboxLayoutEngine& getFlexEngine() const { return m_flexEngine; }
89
90private:
91 QQuickFlexboxLayoutEngine m_flexEngine;
92 QQuickFlexboxLayout::FlexboxDirection m_direction = QQuickFlexboxLayout::Row;
93 QQuickFlexboxLayout::FlexboxWrap m_wrap = QQuickFlexboxLayout::NoWrap;
94 QQuickFlexboxLayout::FlexboxAlignment m_alignItems = QQuickFlexboxLayout::AlignStart;
95 // Align items within the layout in the multi-line containter (i.e. with
96 // wrap enabled) and its aligned to the cross axis of the flexbox layout
97 QQuickFlexboxLayout::FlexboxAlignment m_alignContent = QQuickFlexboxLayout::AlignStart;
98 // Align content item in the multi-line containter and its aligned to the
99 // main axis of the flexbox layout
100 QQuickFlexboxLayout::FlexboxJustify m_justifyContent = QQuickFlexboxLayout::JustifyStart;
101 qreal m_gap = 0.;
102 qreal m_rowGap = 0.;
103 qreal m_columnGap = 0.;
104 std::bitset<QQuickFlexboxLayout::GapMax> m_gapBitSet;
105};
106
107static QQuickFlexboxLayoutAttached *attachedFlexboxLayoutObject(QQuickItem *item, bool create = false)
108{
109 return static_cast<QQuickFlexboxLayoutAttached*>(
110 qmlAttachedPropertiesObject<QQuickFlexboxLayout>(item, create));
111}
112
113QQuickFlexboxLayout::QQuickFlexboxLayout(QQuickItem *parent) :
114 QQuickLayout(*new QQuickFlexboxLayoutPrivate, parent)
115{
116 Q_D(QQuickFlexboxLayout);
117 d->m_flexEngine.setFlexboxParentItem(new QQuickFlexboxLayoutItem(this));
118}
119
120QQuickFlexboxLayout::~QQuickFlexboxLayout()
121{
122 Q_D(QQuickFlexboxLayout);
123 d->m_flexEngine.clearItems();
124}
125
126/*!
127 \qmlproperty enumeration FlexboxLayout::direction
128
129 This property holds the item layout direction within the flex box layout
130 and it defines the
131 \l {https://www.w3.org/TR/css-flexbox-1/#box-model}{main-axis}.
132
133 Possible values:
134
135 \value FlexboxLayout.Row (default) Items are laid out from
136 left to right.
137 \value FlexboxLayout.RowReversed Items are laid out from right to
138 left.
139 \value FlexboxLayout.Column Items are laid out from top to
140 bottom.
141 \value FlexboxLayout.ColumnReversed Items are laid out from bottom to
142 top.
143
144 The default value is \c FlexboxLayout.Row.
145*/
146QQuickFlexboxLayout::FlexboxDirection QQuickFlexboxLayout::direction() const
147{
148 Q_D(const QQuickFlexboxLayout);
149 return d->m_direction;
150}
151
152void QQuickFlexboxLayout::setDirection(QQuickFlexboxLayout::FlexboxDirection direction)
153{
154 Q_D(QQuickFlexboxLayout);
155 if (d->m_direction == direction)
156 return;
157 d->m_direction = direction;
158 invalidate();
159 emit directionChanged();
160}
161
162/*!
163 \qmlproperty enumeration FlexboxLayout::wrap
164
165 This property specifies that the items within the flex box layout can wrap
166 or not and it happens when the children overflow the size of the flex box
167 layout. If the items are wrapped, it will be placed in multiple lines
168 depending on overflow condition as stated. Each line takes up the
169 maximum size of the item along the
170 \l {https://www.w3.org/TR/css-flexbox-1/#box-model}{cross-axis}.
171
172 Possible values:
173
174 \value FlexboxLayout.Wrap Items are wrapped into multiple lines
175 within the flex box layout.
176 \value FlexboxLayout.NoWrap (default) Items are not wrapped and
177 laid out in single line within the
178 flex box layout.
179 \value FlexboxLayout.WrapReverse Items are wrapped into multiple lines
180 within the flex box layout in the
181 reverse direction.
182
183 The default value is \c FlexboxLayout.NoWrap.
184*/
185QQuickFlexboxLayout::FlexboxWrap QQuickFlexboxLayout::wrap() const
186{
187 Q_D(const QQuickFlexboxLayout);
188 return d->m_wrap;
189}
190
191void QQuickFlexboxLayout::setWrap(QQuickFlexboxLayout::FlexboxWrap wrapMode)
192{
193 Q_D(QQuickFlexboxLayout);
194 if (d->m_wrap == wrapMode)
195 return;
196 d->m_wrap = wrapMode;
197 invalidate();
198 emit wrapChanged();
199}
200
201/*!
202 \qmlproperty enumeration FlexboxLayout::alignItems
203
204 This property specifies the alignment of the items within the
205 \l {https://www.w3.org/TR/css-flexbox-1/#flex-lines}{flex lines} of the
206 flex box layout and its aligned along the
207 \l {https://www.w3.org/TR/css-flexbox-1/#box-model}{cross-axis}
208 (which is orthogonal to the main-axis, as defined by the property
209 \l {direction}). This property can be overridden by the
210 items within the flex box layout through the attached property
211 \l {FlexboxLayout::alignSelf}{FlexboxLayout.alignSelf}.
212
213 Possible values:
214
215 \value FlexboxLayout.AlignStart (default) Items are aligned to the
216 start of the flex box layout
217 cross-axis.
218 \value FlexboxLayout.AlignCenter Items are aligned along the center
219 of the flex box layout cross-axis.
220 \value FlexboxLayout.AlignEnd Items are aligned to the end of the
221 flex box layout cross-axis.
222
223 \note The alignments mentioned in possible values are only applicable for
224 the \l{alignItems} property
225
226 The default value is \c FlexboxLayout.AlignStart.
227*/
228QQuickFlexboxLayout::FlexboxAlignment QQuickFlexboxLayout::alignItems() const
229{
230 Q_D(const QQuickFlexboxLayout);
231 return d->m_alignItems;
232}
233
234void QQuickFlexboxLayout::setAlignItems(QQuickFlexboxLayout::FlexboxAlignment alignment)
235{
236 Q_D(QQuickFlexboxLayout);
237 if (alignment >= QQuickFlexboxLayout::AlignStretch || alignment <= QQuickFlexboxLayout::AlignAuto) {
238 qWarning("Not applicable for Flexbox layout container");
239 return;
240 }
241 if (d->m_alignItems == alignment)
242 return;
243 d->m_alignItems = alignment;
244 invalidate();
245 emit alignItemsChanged();
246}
247
248/*!
249 \qmlproperty enumeration FlexboxLayout::alignContent
250
251 This property specifies the distribution of the
252 \l {https://www.w3.org/TR/css-flexbox-1/#flex-lines}{flex lines} along the
253 \l {https://www.w3.org/TR/css-flexbox-1/#box-model}{cross-axis} of the
254 flex box layout.
255
256 Possible values:
257
258 \value FlexboxLayout.AlignStart (default) Flex lines are aligned to
259 the start of the flex box layout.
260 \value FlexboxLayout.AlignCenter Flex lines are aligned along the
261 center of the flex box layout.
262 \value FlexboxLayout.AlignEnd Flex lines are aligned to the end
263 of the flex box layout.
264 \value FlexboxLayout.AlignStretch Flex lines are stretched according
265 to the height of the flex box
266 layout.
267 \value FlexboxLayout.AlignSpaceBetween The spaces are evenly distributed
268 between the lines and no space
269 along the edge of the flex box
270 layout.
271 \value FlexboxLayout.AlignSpaceAround The spaces are evenly distributed
272 between the lines and half-size
273 space on the edges of the flex box
274 layout.
275 \value FlexboxLayout.AlignSpaceEvenly The spaces are evenly distributed
276 between the lines and the edges of
277 the flex box layout. Not supported
278 in Qt 6.10.
279
280 The default value is \c FlexboxLayout.AlignStart.
281*/
282QQuickFlexboxLayout::FlexboxAlignment QQuickFlexboxLayout::alignContent() const
283{
284 Q_D(const QQuickFlexboxLayout);
285 return d->m_alignContent;
286}
287
288void QQuickFlexboxLayout::setAlignContent(QQuickFlexboxLayout::FlexboxAlignment alignment)
289{
290 Q_D(QQuickFlexboxLayout);
291 if (alignment == QQuickFlexboxLayout::AlignSpaceEvenly) {
292 qmlWarning(this) << "Currently not supported for Flexbox layout container";
293 return;
294 }
295 if (d->m_alignContent == alignment)
296 return;
297 d->m_alignContent = alignment;
298 invalidate();
299 emit alignContentChanged();
300}
301
302/*!
303 \qmlproperty enumeration FlexboxLayout::justifyContent
304
305 This property specifies the distribution of the items along the
306 \l {https://www.w3.org/TR/css-flexbox-1/#box-model}{main-axis} of the
307 flex box layout.
308
309 Possible values:
310
311 \value FlexboxLayout.JustifyStart (default) Items are aligned to
312 the start of the flex box
313 layout.
314 \value FlexboxLayout.JustifyCenter Items are aligned along the
315 center of the flex box layout.
316 \value FlexboxLayout.JustifyEnd Items are aligned to the end of
317 the flex box layout.
318 \value FlexboxLayout.JustifySpaceBetween The spaces are evenly
319 distributed between the items
320 and no space along the edges
321 of the flex box layout.
322 \value FlexboxLayout.JustifySpaceAround The spaces are evenly
323 distributed between the items
324 and half-size space on the
325 edges of the flex box layout.
326 \value FlexboxLayout.JustiftSpaceEvenly The spaces are evenly
327 distributed between the items
328 and edges of the flex
329 box layout.
330
331 The default value is \c FlexboxLayout.JustifyStart.
332*/
333QQuickFlexboxLayout::FlexboxJustify QQuickFlexboxLayout::justifyContent() const
334{
335 Q_D(const QQuickFlexboxLayout);
336 return d->m_justifyContent;
337}
338
339void QQuickFlexboxLayout::setJustifyContent(QQuickFlexboxLayout::FlexboxJustify justifyContent)
340{
341 Q_D(QQuickFlexboxLayout);
342 if (d->m_justifyContent == justifyContent)
343 return;
344 d->m_justifyContent = justifyContent;
345 invalidate();
346 emit justifyContentChanged();
347}
348
349/*!
350 \qmlproperty real FlexboxLayout::gap
351
352 This property holds the amount of space that need to be applied
353 to the \l {FlexboxLayout} both along the
354 \l {https://www.w3.org/TR/css-align-3/#gaps}{inline axis and block axis}.
355
356 The default value is \c 0.
357*/
358qreal QQuickFlexboxLayout::gap() const
359{
360 Q_D(const QQuickFlexboxLayout);
361 return d->m_gap;
362}
363
364void QQuickFlexboxLayout::setGap(qreal gap)
365{
366 Q_D(QQuickFlexboxLayout);
367 if (d->m_gap == gap)
368 return;
369 d->m_gap = gap;
370 d->m_gapBitSet.set(GapAll);
371 invalidate();
372 emit gapChanged();
373 if (!isGapBitSet(GapRow))
374 emit rowGapChanged();
375 if (!isGapBitSet(GapColumn))
376 emit columnGapChanged();
377}
378
379void QQuickFlexboxLayout::resetGap()
380{
381 Q_D(QQuickFlexboxLayout);
382 d->m_gap = 0;
383 d->m_gapBitSet.reset(GapAll);
384 emit gapChanged();
385 if (!isGapBitSet(GapRow))
386 emit rowGapChanged();
387 if (!isGapBitSet(GapColumn))
388 emit columnGapChanged();
389}
390
391/*!
392 \qmlproperty real FlexboxLayout::rowGap
393
394 This property holds the amount of space that need to be applied to the
395 \l {FlexboxLayout} along the
396 \l {https://www.w3.org/TR/css-align-3/#gaps}{block axis}. Setting this
397 property overrides the \l{gap} value affecting the
398 \l {https://www.w3.org/TR/css-align-3/#gaps}{block axis}.
399
400 The default value is \c 0.
401*/
402qreal QQuickFlexboxLayout::rowGap() const
403{
404 Q_D(const QQuickFlexboxLayout);
405 if (!isGapBitSet(GapRow))
406 return d->m_gap;
407 return d->m_rowGap;
408}
409
410void QQuickFlexboxLayout::setRowGap(qreal gap)
411{
412 Q_D(QQuickFlexboxLayout);
413 if (d->m_rowGap == gap)
414 return;
415 d->m_rowGap = gap;
416 d->m_gapBitSet.set(QQuickFlexboxLayout::GapRow);
417 invalidate();
418 emit rowGapChanged();
419}
420
421void QQuickFlexboxLayout::resetRowGap()
422{
423 Q_D(QQuickFlexboxLayout);
424 d->m_rowGap = 0;
425 d->m_gapBitSet.reset(GapRow);
426 emit rowGapChanged();
427}
428
429/*!
430 \qmlproperty real FlexboxLayout::columnGap
431
432 This property holds the amount of space that need to be applied
433 to the \l {FlexboxLayout} along the
434 \l {https://www.w3.org/TR/css-align-3/#gaps}{inline axis}. Setting this
435 property override the \l{gap} value affecting the
436 \l {https://www.w3.org/TR/css-align-3/#gaps}{inline axis}.
437
438 The default value is \c 0.
439*/
440qreal QQuickFlexboxLayout::columnGap() const
441{
442 Q_D(const QQuickFlexboxLayout);
443 if (!isGapBitSet(GapColumn))
444 return d->m_gap;
445 return d->m_columnGap;
446}
447
448void QQuickFlexboxLayout::setColumnGap(qreal gap)
449{
450 Q_D(QQuickFlexboxLayout);
451 if (d->m_columnGap == gap)
452 return;
453 d->m_columnGap = gap;
454 d->m_gapBitSet.set(QQuickFlexboxLayout::GapColumn);
455 invalidate();
456 emit columnGapChanged();
457}
458
459void QQuickFlexboxLayout::resetColumnGap()
460{
461 Q_D(QQuickFlexboxLayout);
462 d->m_columnGap = 0;
463 d->m_gapBitSet.reset(GapColumn);
464 emit columnGapChanged();
465}
466
467void QQuickFlexboxLayout::updateLayoutItems()
468{
469 Q_D(QQuickFlexboxLayout);
470 // Clean all the items in the layout
471 d->m_flexEngine.clearItems();
472 // Update the parent item properties
473 if (auto *flexParentItem = d->m_flexEngine.getFlexboxParentItem()) {
474 flexParentItem->setFlexDirection(d->m_direction);
475 flexParentItem->setFlexWrap(d->m_wrap);
476 flexParentItem->setFlexAlignItemsProperty(d->m_alignItems);
477 flexParentItem->setFlexAlignContentProperty(d->m_alignContent);
478 flexParentItem->setFlexJustifyContentProperty(d->m_justifyContent);
479 if (isGapBitSet(QQuickFlexboxLayout::GapAll))
480 flexParentItem->setFlexGap(QQuickFlexboxLayout::GapAll, d->m_gap);
481 if (isGapBitSet(QQuickFlexboxLayout::GapRow))
482 flexParentItem->setFlexGap(QQuickFlexboxLayout::GapRow, d->m_rowGap);
483 if (isGapBitSet(QQuickFlexboxLayout::GapColumn))
484 flexParentItem->setFlexGap(QQuickFlexboxLayout::GapColumn, d->m_columnGap);
485 }
486
487 // Insert the items in the layout
488 const QList<QQuickItem *> items = childItems();
489 for (auto *childItem : items) {
490 Q_ASSERT(childItem);
491 checkAnchors(childItem);
492 if (shouldIgnoreItem(childItem))
493 continue;
494 // Create and set the attached properties of the flex item and add it as child
495 auto *flexLayoutItem = new QQuickFlexboxLayoutItem(childItem);
496 if (auto *flexItemAttachedProperties = attachedFlexboxLayoutObject(childItem))
497 flexLayoutItem->setFlexAlignSelfProperty(flexItemAttachedProperties->alignSelf());
498 d->m_flexEngine.insertItem(flexLayoutItem);
499 }
500}
501
502void QQuickFlexboxLayout::checkAnchors(QQuickItem *item) const
503{
504 QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors;
505 if (anchors && anchors->activeDirections())
506 qmlWarning(item) << "Detected anchors on an item that is managed by a layout. This is undefined behavior; use FlexboxLayout alignment properties instead.";
507}
508
509void QQuickFlexboxLayout::componentComplete()
510{
511 QQuickLayout::componentComplete();
512 ensureLayoutItemsUpdated(ApplySizeHints);
513 if (qobject_cast<QQuickLayout*>(parentItem()))
514 return;
515 rearrange(QSizeF(width(), height()));
516}
517
518void QQuickFlexboxLayout::itemVisibilityChanged(QQuickItem *item)
519{
520 if (!isReady())
521 return;
522 invalidate(item);
523}
524
525QSizeF QQuickFlexboxLayout::sizeHint(Qt::SizeHint whichSizeHint) const
526{
527 Q_D(const QQuickFlexboxLayout);
528 QSizeF sizeHint = d->m_flexEngine.sizeHint(whichSizeHint);
529 d->m_dirty = false;
530 return sizeHint;
531}
532
533QQuickFlexboxLayoutAttached *QQuickFlexboxLayout::qmlAttachedProperties(QObject *object)
534{
535 return new QQuickFlexboxLayoutAttached(object);
536}
537
538bool QQuickFlexboxLayout::isGapBitSet(QQuickFlexboxLayout::FlexboxGap gap) const
539{
540 Q_D(const QQuickFlexboxLayout);
541 if (gap < QQuickFlexboxLayout::GapRow || gap > QQuickFlexboxLayout::GapAll)
542 return false;
543 return d->m_gapBitSet[gap];
544}
545
546QQuickItem *QQuickFlexboxLayout::itemAt(int index) const
547{
548 const auto items = childItems();
549 for (QQuickItem *item : items) {
550 if (shouldIgnoreItem(item))
551 continue;
552 if (index == 0)
553 return item;
554 --index;
555 }
556 return nullptr;
557}
558
559int QQuickFlexboxLayout::itemCount() const
560{
561 int count = 0;
562 const auto items = childItems();
563 for (QQuickItem *item : items) {
564 if (shouldIgnoreItem(item))
565 continue;
566 ++count;
567 }
568 return count;
569}
570
571void QQuickFlexboxLayout::invalidate(QQuickItem *childItem)
572{
573 Q_D(QQuickFlexboxLayout);
574 d->m_flexEngine.invalidateItemSizeHint(childItem);
575 QQuickLayout::invalidate(this);
576 if (QQuickLayout *parentLayout = qobject_cast<QQuickLayout *>(parentItem()))
577 parentLayout->invalidate(this);
578}
579
580void QQuickFlexboxLayout::childItemsChanged()
581{
582 const int count = itemCount();
583 for (int i = 0; i < count; ++i) {
584 QQuickItem *child = itemAt(i);
585 checkAnchors(child);
586 }
587}
588
589void QQuickFlexboxLayout::rearrange(const QSizeF &newSize)
590{
591 Q_D(QQuickFlexboxLayout);
592 if (newSize.isNull() || !newSize.isValid())
593 return;
594 d->m_flexEngine.setGeometries(newSize);
595 QQuickLayout::rearrange(newSize);
596}
597
598void QQuickFlexboxLayout::itemSiblingOrderChanged(QQuickItem *)
599{
600 if (!isReady())
601 return;
602 invalidate();
603}
604
605QQuickFlexboxLayoutAttached::QQuickFlexboxLayoutAttached(QObject *object)
606{
607 auto item = qobject_cast<QQuickItem*>(object);
608 if (!item) {
609 qmlWarning(object) << "FlexboxLayout attached property must be attached to an object deriving from Item";
610 return;
611 }
612 setParent(object);
613 if (auto flexboxLayout = qobject_cast<QQuickFlexboxLayout*>(item->parentItem())) {
614 if (!flexboxLayout->isComponentComplete()) {
615 // Don't try to get the index if the FlexboxLayout itself hasn't
616 // loaded yet.
617 return;
618 }
619 // In case of lazy loading in loader, attachedProperties are created
620 // and updated for the object after adding the child object to the
621 // stack layout, which leads to entries with same index. Triggering
622 // childItemsChanged() resets to right index in the stack layout.
623 flexboxLayout->childItemsChanged();
624 }
625}
626
627/*!
628 \qmlattachedproperty enumeration FlexboxLayout::alignSelf
629
630 This attached property allows to align this item in the flex box layout
631 along the \l {https://www.w3.org/TR/css-flexbox-1/#box-model}{cross-axis}
632 and it overrides the parent flex box layout property
633 \l{alignItems}.
634
635 By default, the child item inherit the alignment from the parent and it can
636 override the parent flex box layout
637 \l{alignItems} property with the values
638 \c {FlexboxLayout.AlignStart}, \c {FlexboxLayout.AlignCenter}
639 and \c {FlexboxLayout.AlignEnd}.
640
641 The default value is \c {FlexboxLayout.AlignAuto}.
642*/
643QQuickFlexboxLayout::FlexboxAlignment QQuickFlexboxLayoutAttached::alignSelf() const
644{
645 return m_alignSelf;
646}
647
648void QQuickFlexboxLayoutAttached::setAlignSelf(QQuickFlexboxLayout::FlexboxAlignment alignment)
649{
650 if (m_alignSelf == alignment)
651 return;
652
653 m_alignSelf = alignment;
654 const auto *item = qobject_cast<QQuickItem*>(parent());
655 if (auto *flexLayout = qobject_cast<QQuickFlexboxLayout *>(item->parent())) {
656 auto *priv = dynamic_cast<QQuickFlexboxLayoutPrivate *>(QQuickLayoutPrivate::get(flexLayout));
657 auto &flexEngine = priv->getFlexEngine();
658 auto *item = qobject_cast<QQuickItem *>(parent());
659 if (auto *flexLayoutItem = flexEngine.findFlexboxLayoutItem(item)) {
660 flexLayoutItem->setFlexAlignSelfProperty(alignment);
661 flexLayout->invalidate();
662 }
663 }
664 emit alignSelfChanged();
665}
666
667QT_END_NAMESPACE
668
669#include "moc_qquickflexboxlayout_p.cpp"
static QQuickFlexboxLayoutAttached * attachedFlexboxLayoutObject(QQuickItem *item, bool create=false)