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 ());
91 size += QSize { margin () * 2, margin () * 2 };
97 QLayout::setGeometry (rect);
98 DoLayout (rect,
false);
106 int FlowLayout::DoLayout (
const QRect& rect,
bool testOnly)
const
108 int left = 0, top = 0, right = 0, bottom = 0;
109 getContentsMargins (&left, &top, &right, &bottom);
111 const auto& effectiveRect = rect.adjusted (left, top, -right, -bottom);
112 int x = effectiveRect.x ();
113 int y = effectiveRect.y ();
116 for (
const auto item : ItemList_)
118 const auto widget = item->widget ();
122 spaceX = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
123 QSizePolicy::PushButton, Qt::Horizontal);
126 spaceY = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
127 QSizePolicy::PushButton, Qt::Vertical);
129 const auto&
sizeHint = item->sizeHint ();
130 const int hintWidth =
sizeHint.width ();
131 int nextX = x + hintWidth + spaceX;
132 if (nextX - spaceX > effectiveRect.right () &&
135 x = effectiveRect.x ();
136 y += lineHeight + spaceY;
137 nextX = x + hintWidth + spaceX;
142 item->setGeometry ({ { x, y },
sizeHint });
145 lineHeight = std::max (lineHeight,
sizeHint.height ());
148 return y + lineHeight - rect.y () + bottom;
151 int FlowLayout::SmartSpacing (QStyle::PixelMetric pm)
const
153 const auto obj = parent ();
157 if (
const auto pw =
dynamic_cast<QWidget*
> (obj))
158 return pw->style ()->pixelMetric (pm,
nullptr, pw);
159 if (
const auto lay =
dynamic_cast<QLayout*
> (obj))
160 return lay->spacing ();
A simple flow layout implementation.
FlowLayout(QWidget *, int=-1, int=-1, int=-1)
QSize minimumSize() const override
QLayoutItem * takeAt(int) override
int verticalSpacing() const
void addItem(QLayoutItem *) override
int count() const override
bool hasHeightForWidth() const override
int heightForWidth(int) const override
QLayoutItem * itemAt(int) const override
int horizontalSpacing() const
QSize sizeHint() const override
void setGeometry(const QRect &) override
Qt::Orientations expandingDirections() const override