38 int margin,
int hspace,
int vspace)
43 setContentsMargins (margin, margin, margin, margin);
47 :
FlowLayout {
nullptr, margin, hspace, vspace }
53 while (
const auto item =
takeAt (0))
66 SmartSpacing (QStyle::PM_LayoutHorizontalSpacing);
73 SmartSpacing (QStyle::PM_LayoutVerticalSpacing);
88 return DoLayout ({ 0, 0, width, 0 },
true);
93 return ItemList_.size ();
98 return ItemList_.value (idx);
103 if (idx >= 0 && idx < ItemList_.size ())
104 return ItemList_.takeAt (idx);
112 for (
const auto item : ItemList_)
113 size = size.expandedTo (item->minimumSize ());
115 size += QSize { margin () * 2, margin () * 2 };
121 QLayout::setGeometry (rect);
122 DoLayout (rect,
false);
130 int FlowLayout::DoLayout (
const QRect& rect,
bool testOnly)
const 132 int left = 0, top = 0, right = 0, bottom = 0;
133 getContentsMargins (&left, &top, &right, &bottom);
135 const auto& effectiveRect = rect.adjusted (left, top, -right, -bottom);
136 int x = effectiveRect.x ();
137 int y = effectiveRect.y ();
140 for (
const auto item : ItemList_)
142 const auto widget = item->widget ();
146 spaceX = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
147 QSizePolicy::PushButton, Qt::Horizontal);
150 spaceY = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
151 QSizePolicy::PushButton, Qt::Vertical);
153 const auto&
sizeHint = item->sizeHint ();
154 const int hintWidth =
sizeHint.width ();
155 int nextX = x + hintWidth + spaceX;
156 if (nextX - spaceX > effectiveRect.right () &&
159 x = effectiveRect.x ();
160 y += lineHeight + spaceY;
161 nextX = x + hintWidth + spaceX;
166 item->setGeometry ({ { x, y },
sizeHint });
172 return y + lineHeight - rect.y () + bottom;
175 int FlowLayout::SmartSpacing (QStyle::PixelMetric pm)
const 177 const auto obj = parent ();
180 else if (obj->isWidgetType ())
182 const auto pw =
static_cast<QWidget*
> (obj);
183 return pw->style ()->pixelMetric (pm, 0, pw);
186 return static_cast<QLayout*
> (obj)->spacing ();
QSize sizeHint() const override
FlowLayout(QWidget *, int=-1, int=-1, int=-1)
constexpr detail::AggregateType< detail::AggregateFunction::Max, Ptr > max
bool hasHeightForWidth() const override
int verticalSpacing() const
QLayoutItem * itemAt(int) const override
QSize minimumSize() const override
void addItem(QLayoutItem *) override
void setGeometry(const QRect &) override
Qt::Orientations expandingDirections() const override
A simple flow layout implementation.
QLayoutItem * takeAt(int) override
int count() const override
int heightForWidth(int) const override
int horizontalSpacing() const