15 int margin,
int hspace,
int vspace)
20 setContentsMargins (margin, margin, margin, margin);
24 :
FlowLayout {
nullptr, margin, hspace, vspace }
30 qDeleteAll (ItemList_);
42 SmartSpacing (QStyle::PM_LayoutHorizontalSpacing);
49 SmartSpacing (QStyle::PM_LayoutVerticalSpacing);
64 return DoLayout ({ 0, 0, width, 0 },
true);
69 return ItemList_.size ();
74 return ItemList_.value (idx);
79 if (idx < 0 || idx >= ItemList_.size ())
82 return ItemList_.takeAt (idx);
88 for (
const auto item : ItemList_)
89 size = size.expandedTo (item->minimumSize ());
95 getContentsMargins (&left, &top, &right, &bottom);
96 size += QSize { left + right, top + bottom };
102 QLayout::setGeometry (rect);
103 DoLayout (rect,
false);
111 int FlowLayout::DoLayout (
const QRect& rect,
bool testOnly)
const
113 int left = 0, top = 0, right = 0, bottom = 0;
114 getContentsMargins (&left, &top, &right, &bottom);
116 const auto& effectiveRect = rect.adjusted (left, top, -right, -bottom);
117 int x = effectiveRect.x ();
118 int y = effectiveRect.y ();
121 for (
const auto item : ItemList_)
123 const auto widget = item->widget ();
127 spaceX = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
128 QSizePolicy::PushButton, Qt::Horizontal);
131 spaceY = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
132 QSizePolicy::PushButton, Qt::Vertical);
134 const auto&
sizeHint = item->sizeHint ();
135 const int hintWidth =
sizeHint.width ();
136 int nextX = x + hintWidth + spaceX;
137 if (nextX - spaceX > effectiveRect.right () &&
140 x = effectiveRect.x ();
141 y += lineHeight + spaceY;
142 nextX = x + hintWidth + spaceX;
147 item->setGeometry ({ { x, y },
sizeHint });
153 return y + lineHeight - rect.y () + bottom;
156 int FlowLayout::SmartSpacing (QStyle::PixelMetric pm)
const
158 const auto obj = parent ();
162 if (
const auto pw =
dynamic_cast<QWidget*
> (obj))
163 return pw->style ()->pixelMetric (pm,
nullptr, pw);
164 if (
const auto lay =
dynamic_cast<QLayout*
> (obj))
165 return lay->spacing ();