Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qlayout.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
4#include "qlayout.h"
5
6#include "qapplication.h"
7#include "qlayoutengine_p.h"
8#if QT_CONFIG(menubar)
9#include "qmenubar.h"
10#endif
11#if QT_CONFIG(toolbar)
12#include "qtoolbar.h"
13#endif
14#if QT_CONFIG(sizegrip)
15#include "qsizegrip.h"
16#endif
17#include "qevent.h"
18#include "qstyle.h"
19#include "qvariant.h"
20#include "qwidget_p.h"
21#include "qlayout_p.h"
22
24
25static int menuBarHeightForWidth(QWidget *menubar, int w)
26{
27 if (menubar && !menubar->isHidden() && !menubar->isWindow()) {
28 int result = menubar->heightForWidth(qMax(w, menubar->minimumWidth()));
29 if (result == -1)
30 result = menubar->sizeHint().height();
31 const int min = qSmartMinSize(menubar).height();
32 result = qBound(min, result, menubar->maximumSize().height());
33 if (result != -1)
34 return result;
35 }
36 return 0;
37}
38
84 : QObject(*new QLayoutPrivate, parent)
85{
86 if (!parent)
87 return;
88 parent->setLayout(this);
89}
90
94 : QObject(dd, lay ? static_cast<QObject*>(lay) : static_cast<QObject*>(w))
95{
96 Q_D(QLayout);
97 if (lay) {
98 lay->addItem(this);
99 } else if (w) {
100 if (Q_UNLIKELY(w->layout())) {
101 qWarning("QLayout: Attempting to add QLayout \"%ls\" to %s \"%ls\", which"
102 " already has a layout",
103 qUtf16Printable(QObject::objectName()), w->metaObject()->className(),
104 qUtf16Printable(w->objectName()));
105 setParent(nullptr);
106 } else {
107 d->topLevel = true;
108 w->d_func()->layout = this;
109 QT_TRY {
110 invalidate();
111 } QT_CATCH(...) {
112 w->d_func()->layout = nullptr;
114 }
115 }
116 }
117}
118
120 : QObjectPrivate(), insideSpacing(-1), userLeftMargin(-1), userTopMargin(-1), userRightMargin(-1),
121 userBottomMargin(-1), topLevel(false), enabled(true), activated(true), autoNewChild(false),
122 constraint(QLayout::SetDefaultConstraint), menubar(nullptr)
123{
124}
125
126void QLayoutPrivate::getMargin(int *result, int userMargin, QStyle::PixelMetric pm) const
127{
128 if (!result)
129 return;
130
131 Q_Q(const QLayout);
132 if (userMargin >= 0) {
133 *result = userMargin;
134 } else if (!topLevel) {
135 *result = 0;
136 } else if (QWidget *pw = q->parentWidget()) {
137 *result = pw->style()->pixelMetric(pm, nullptr, pw);
138 } else {
139 *result = 0;
140 }
141}
142
143// Static item factory functions that allow for hooking things in Designer
144
147
155
157{
159 if (QSpacerItem *si = (*spacerItemFactoryMethod)(layout, w, h, hPolicy, vPolicy))
160 return si;
161 return new QSpacerItem(w, h, hPolicy, vPolicy);
162}
163
164
165
191
192
193
200{
201 int i = 0;
203 while (item) {
204 if (item->widget() == w) {
205 item->setAlignment(alignment);
206 invalidate();
207 return true;
208 }
209 ++i;
210 item = itemAt(i);
211 }
212 return false;
213}
214
223{
224 int i = 0;
226 while (item) {
227 if (item->layout() == l) {
228 item->setAlignment(alignment);
229 invalidate();
230 return true;
231 }
232 ++i;
233 item = itemAt(i);
234 }
235 return false;
236}
237
256{
257 Q_D(const QLayout);
258 if (d->insideSpacing >=0) {
259 return d->insideSpacing;
260 } else {
261 // arbitrarily prefer horizontal spacing to vertical spacing
263 }
264}
265
267{
268 Q_D(QLayout);
269 d->insideSpacing = spacing;
270 invalidate();
271}
272
289{
290 Q_D(QLayout);
291
292 if (d->userLeftMargin == left && d->userTopMargin == top &&
293 d->userRightMargin == right && d->userBottomMargin == bottom)
294 return;
295
296 d->userLeftMargin = left;
297 d->userTopMargin = top;
298 d->userRightMargin = right;
299 d->userBottomMargin = bottom;
300 invalidate();
301}
302
314{
315 setContentsMargins(margins.left(), margins.top(), margins.right(), margins.bottom());
316}
317
327{
328 setContentsMargins(-1, -1, -1, -1);
329}
330
347void QLayout::getContentsMargins(int *left, int *top, int *right, int *bottom) const
348{
349 Q_D(const QLayout);
350 d->getMargin(left, d->userLeftMargin, QStyle::PM_LayoutLeftMargin);
351 d->getMargin(top, d->userTopMargin, QStyle::PM_LayoutTopMargin);
352 d->getMargin(right, d->userRightMargin, QStyle::PM_LayoutRightMargin);
353 d->getMargin(bottom, d->userBottomMargin, QStyle::PM_LayoutBottomMargin);
354}
355
372
382{
383 Q_D(const QLayout);
384 int left, top, right, bottom;
386 return d->rect.adjusted(+left, +top, -right, -bottom);
387}
388
389
400{
401 Q_D(const QLayout);
402 if (!d->topLevel) {
403 if (parent()) {
404 QLayout *parentLayout = qobject_cast<QLayout*>(parent());
405 if (Q_UNLIKELY(!parentLayout)) {
406 qWarning("QLayout::parentWidget: A layout can only have another layout as a parent.");
407 return nullptr;
408 }
409 return parentLayout->parentWidget();
410 } else {
411 return nullptr;
412 }
413 } else {
415 return static_cast<QWidget *>(parent());
416 }
417}
418
423{
424 int i = 0;
426 while (item) {
427 if (!item->isEmpty())
428 return false;
429 ++i;
430 item = itemAt(i);
431 }
432 return true;
433}
434
438QSizePolicy::ControlTypes QLayout::controlTypes() const
439{
440 if (count() == 0)
442 QSizePolicy::ControlTypes types;
443 for (int i = count() - 1; i >= 0; --i)
444 types |= itemAt(i)->controlTypes();
445 return types;
446}
447
452{
453 Q_D(QLayout);
454 d->rect = r;
455}
456
461{
462 Q_D(const QLayout);
463 return d->rect;
464}
465
470{
471 Q_D(QLayout);
472 d->rect = QRect();
473 update();
474}
475
477{
478 QLayout *lay = li->layout();
479 if (!lay)
480 return false;
481 int i = 0;
483 while ((child = lay->itemAt(i))) {
484 if (child->widget() == w) {
485 delete lay->takeAt(i);
486 lay->invalidate();
487 return true;
488 } else if (removeWidgetRecursively(child, w)) {
489 return true;
490 } else {
491 ++i;
492 }
493 }
494 return false;
495}
496
497
499{
500 Q_Q(QLayout);
501 QWidget *mw = q->parentWidget();
502 QRect rect = mw->testAttribute(Qt::WA_LayoutOnEntireRect) ? mw->rect() : mw->contentsRect();
503 const int mbh = menuBarHeightForWidth(menubar, rect.width());
504 const int mbTop = rect.top();
505 rect.setTop(mbTop + mbh);
506 q->setGeometry(rect);
507#if QT_CONFIG(menubar)
508 if (menubar)
509 menubar->setGeometry(rect.left(), mbTop, rect.width(), mbh);
510#endif
511}
512
513
521{
522 Q_D(QLayout);
523 const QEvent::Type type = e->type();
524 if (!d->enabled && type != QEvent::ChildRemoved)
525 return;
526
527 switch (type) {
528 case QEvent::Resize:
529 if (d->activated)
530 d->doResize();
531 else
532 activate();
533 break;
535 {
536 QChildEvent *c = (QChildEvent *)e;
537 QObject *child = c->child();
539 if (op->wasWidget) {
540#if QT_CONFIG(menubar)
541 if (child == d->menubar)
542 d->menubar = nullptr;
543#endif
545 }
546 }
547 break;
549 if (static_cast<QWidget *>(parent())->isVisible())
550 activate();
551 break;
552 default:
553 break;
554 }
555}
556
561{
562 Q_D(QLayout);
563 if (!d->enabled)
564 return;
565
566 if (e->type() != QEvent::ChildRemoved)
567 return;
568
569 if (QLayout *childLayout = qobject_cast<QLayout *>(e->child()))
570 removeItem(childLayout);
571}
572
578{
579 Q_D(const QLayout);
580 int side=0, top=0;
581 if (d->topLevel) {
583 parent->ensurePolished();
584 QWidgetPrivate *wd = parent->d_func();
585 side += wd->leftmargin + wd->rightmargin;
586 top += wd->topmargin + wd->bottommargin;
587 }
588 int h = minimumHeightForWidth(w - side) + top;
589#if QT_CONFIG(menubar)
590 h += menuBarHeightForWidth(d->menubar, w);
591#endif
592 return h;
593}
594
600{
601 Q_D(const QLayout);
602 int side=0, top=0;
603 if (d->topLevel) {
605 parent->ensurePolished();
606 QWidgetPrivate *wd = parent->d_func();
607 side += wd->leftmargin + wd->rightmargin;
608 top += wd->topmargin + wd->bottommargin;
609 }
610 int h = heightForWidth(w - side) + top;
611#if QT_CONFIG(menubar)
612 h += menuBarHeightForWidth(d->menubar, w);
613#endif
614 return h;
615}
616
622{
623 Q_D(const QLayout);
624 int side=0, top=0;
625 if (d->topLevel) {
626 QWidget *pw = parentWidget();
627 pw->ensurePolished();
628 QWidgetPrivate *wd = pw->d_func();
629 side += wd->leftmargin + wd->rightmargin;
630 top += wd->topmargin + wd->bottommargin;
631 }
632
633 QSize s = minimumSize();
634#if QT_CONFIG(menubar)
635 top += menuBarHeightForWidth(d->menubar, s.width() + side);
636#endif
637 return s + QSize(side, top);
638}
639
645{
646 Q_D(const QLayout);
647 int side=0, top=0;
648 if (d->topLevel) {
649 QWidget *pw = parentWidget();
650 pw->ensurePolished();
651 QWidgetPrivate *wd = pw->d_func();
652 side += wd->leftmargin + wd->rightmargin;
653 top += wd->topmargin + wd->bottommargin;
654 }
655
656 QSize s = sizeHint();
657 if (hasHeightForWidth())
658 s.setHeight(heightForWidth(s.width() + side));
659#if QT_CONFIG(menubar)
660 top += menuBarHeightForWidth(d->menubar, s.width());
661#endif
662 return s + QSize(side, top);
663}
664
670{
671 Q_D(const QLayout);
672 int side=0, top=0;
673 if (d->topLevel) {
674 QWidget *pw = parentWidget();
675 pw->ensurePolished();
676 QWidgetPrivate *wd = pw->d_func();
677 side += wd->leftmargin + wd->rightmargin;
678 top += wd->topmargin + wd->bottommargin;
679 }
680
681 QSize s = maximumSize();
682#if QT_CONFIG(menubar)
683 top += menuBarHeightForWidth(d->menubar, s.width());
684#endif
685
686 if (d->topLevel)
687 s = QSize(qMin(s.width() + side, QLAYOUTSIZE_MAX),
688 qMin(s.height() + top, QLAYOUTSIZE_MAX));
689 return s;
690}
691
701{
702 Q_D(QLayout);
703 if (d->topLevel && parent() && parent()->isWidgetType() && parentWidget()->layout() == this)
704 parentWidget()->d_func()->layout = nullptr;
705 else if (QLayout *parentLayout = qobject_cast<QLayout *>(parent()))
706 parentLayout->removeItem(this);
707}
708
709
720{
721 if (Q_UNLIKELY(childLayout->parent())) {
722 qWarning("QLayout::addChildLayout: layout %s \"%ls\" already has a parent",
723 childLayout->metaObject()->className(), qUtf16Printable(childLayout->objectName()));
724 return;
725 }
726 childLayout->setParent(this);
727
728 if (QWidget *mw = parentWidget()) {
729 childLayout->d_func()->reparentChildWidgets(mw);
730 }
731
732}
733
738{
739 const bool ok = !layout->parent();
741 return ok;
742}
743
744#ifdef QT_DEBUG
745static bool layoutDebug()
746{
747 static int checked_env = -1;
748 if (checked_env == -1)
749 checked_env = !!qEnvironmentVariableIntValue("QT_LAYOUT_DEBUG");
750
751 return checked_env;
752}
753#endif
754
756{
757 Q_Q(QLayout);
758 int n = q->count();
759
760#if QT_CONFIG(menubar)
761 if (menubar && menubar->parentWidget() != mw) {
762 menubar->setParent(mw);
763 }
764#endif
765 bool mwVisible = mw && mw->isVisible();
766 for (int i = 0; i < n; ++i) {
767 QLayoutItem *item = q->itemAt(i);
768 if (QWidget *w = item->widget()) {
769 QWidget *pw = w->parentWidget();
770#ifdef QT_DEBUG
771 if (Q_UNLIKELY(pw && pw != mw && layoutDebug())) {
772 qWarning("QLayout::addChildLayout: widget %s \"%ls\" in wrong parent; moved to correct parent",
773 w->metaObject()->className(), qUtf16Printable(w->objectName()));
774 }
775#endif
776 bool needShow = mwVisible && !QWidgetPrivate::get(w)->isExplicitlyHidden();
777 if (pw != mw)
778 w->setParent(mw);
779 if (needShow)
780 QMetaObject::invokeMethod(w, "_q_showIfNotHidden", Qt::QueuedConnection); //show later
781 } else if (QLayout *l = item->layout()) {
782 l->d_func()->reparentChildWidgets(mw);
783 }
784 }
785}
786
792{
793 Q_Q(const QLayout);
794 if (Q_UNLIKELY(!widget)) {
795 qWarning("QLayout: Cannot add a null widget to %s/%ls", q->metaObject()->className(),
796 qUtf16Printable(q->objectName()));
797 return false;
798 }
799 if (Q_UNLIKELY(widget == q->parentWidget())) {
800 qWarning("QLayout: Cannot add parent widget %s/%ls to its child layout %s/%ls",
801 widget->metaObject()->className(), qUtf16Printable(widget->objectName()),
802 q->metaObject()->className(), qUtf16Printable(q->objectName()));
803 return false;
804 }
805 return true;
806}
807
812bool QLayoutPrivate::checkLayout(QLayout *otherLayout) const
813{
814 Q_Q(const QLayout);
815 if (Q_UNLIKELY(!otherLayout)) {
816 qWarning("QLayout: Cannot add a null layout to %s/%ls",
817 q->metaObject()->className(), qUtf16Printable(q->objectName()));
818 return false;
819 }
820 if (Q_UNLIKELY(otherLayout == q)) {
821 qWarning("QLayout: Cannot add layout %s/%ls to itself",
822 q->metaObject()->className(), qUtf16Printable(q->objectName()));
823 return false;
824 }
825 return true;
826}
827
838{
839 QWidget *mw = parentWidget();
840 QWidget *pw = w->parentWidget();
841
842 //Qt::WA_LaidOut is never reset. It only means that the widget at some point has
843 //been in a layout.
844 if (pw && w->testAttribute(Qt::WA_LaidOut)) {
845 QLayout *l = pw->layout();
846 if (l && removeWidgetRecursively(l, w)) {
847#ifdef QT_DEBUG
848 if (Q_UNLIKELY(layoutDebug()))
849 qWarning("QLayout::addChildWidget: %s \"%ls\" is already in a layout; moved to new layout",
850 w->metaObject()->className(), qUtf16Printable(w->objectName()));
851#endif
852 }
853 }
854 if (pw && mw && pw != mw) {
855#ifdef QT_DEBUG
856 if (Q_UNLIKELY(layoutDebug()))
857 qWarning("QLayout::addChildWidget: %s \"%ls\" in wrong parent; moved to correct parent",
858 w->metaObject()->className(), qUtf16Printable(w->objectName()));
859#endif
860 pw = nullptr;
861 }
862 bool needShow = mw && mw->isVisible() && !QWidgetPrivate::get(w)->isExplicitlyHidden();
863 if (!pw && mw)
864 w->setParent(mw);
865 w->setAttribute(Qt::WA_LaidOut);
866 if (needShow)
867 QMetaObject::invokeMethod(w, "_q_showIfNotHidden", Qt::QueuedConnection); //show later
868}
869
870
871
872
873
874
875
876
883{
884 Q_D(QLayout);
885 if (widget)
887 d->menubar = widget;
888}
889
896{
897 Q_D(const QLayout);
898 return d->menubar;
899}
900
901
913{
914 return QSize(0, 0);
915}
916
931
944Qt::Orientations QLayout::expandingDirections() const
945{
947}
948
949void QLayout::activateRecursiveHelper(QLayoutItem *item)
950{
951 item->invalidate();
952 QLayout *layout = item->layout();
953 if (layout) {
955 int i=0;
956 while ((child = layout->itemAt(i++)))
957 activateRecursiveHelper(child);
958 layout->d_func()->activated = true;
959 }
960}
961
972{
973 QLayout *layout = this;
974 while (layout && layout->d_func()->activated) {
975 layout->d_func()->activated = false;
976 if (layout->d_func()->topLevel) {
978 QWidget *mw = static_cast<QWidget*>(layout->parent());
980 break;
981 }
982 layout = static_cast<QLayout*>(layout->parent());
983 }
984}
985
996{
997 Q_D(QLayout);
998 if (!d->enabled || !parent())
999 return false;
1000 if (!d->topLevel)
1001 return static_cast<QLayout*>(parent())->activate();
1002 if (d->activated)
1003 return false;
1004 QWidget *mw = static_cast<QWidget*>(parent());
1005 if (Q_UNLIKELY(!mw)) {
1006 qWarning("QLayout::activate: %s \"%ls\" does not have a main widget",
1008 return false;
1009 }
1010 activateRecursiveHelper(this);
1011
1012 QWidgetPrivate *md = mw->d_func();
1013 uint explMin = md->extra ? md->extra->explicitMinSize : 0;
1014 uint explMax = md->extra ? md->extra->explicitMaxSize : 0;
1015
1016 switch (d->constraint) {
1017 case SetFixedSize:
1018 // will trigger resize
1019 mw->setFixedSize(totalSizeHint());
1020 break;
1021 case SetMinimumSize:
1022 mw->setMinimumSize(totalMinimumSize());
1023 break;
1024 case SetMaximumSize:
1025 mw->setMaximumSize(totalMaximumSize());
1026 break;
1027 case SetMinAndMaxSize:
1028 mw->setMinimumSize(totalMinimumSize());
1029 mw->setMaximumSize(totalMaximumSize());
1030 break;
1031 case SetDefaultConstraint: {
1032 bool widthSet = explMin & Qt::Horizontal;
1033 bool heightSet = explMin & Qt::Vertical;
1034 if (mw->isWindow()) {
1035 QSize ms = totalMinimumSize();
1036 if (widthSet)
1037 ms.setWidth(mw->minimumSize().width());
1038 if (heightSet)
1039 ms.setHeight(mw->minimumSize().height());
1040 mw->setMinimumSize(ms);
1041 } else if (!widthSet || !heightSet) {
1042 QSize ms = mw->minimumSize();
1043 if (!widthSet)
1044 ms.setWidth(0);
1045 if (!heightSet)
1046 ms.setHeight(0);
1047 mw->setMinimumSize(ms);
1048 }
1049 break;
1050 }
1051 case SetNoConstraint:
1052 break;
1053 }
1054
1055 d->doResize();
1056
1057 if (md->extra) {
1058 md->extra->explicitMinSize = explMin;
1059 md->extra->explicitMaxSize = explMax;
1060 }
1061 // ideally only if sizeHint() or sizePolicy() has changed
1062 mw->updateGeometry();
1063 return true;
1064}
1065
1090QLayoutItem *QLayout::replaceWidget(QWidget *from, QWidget *to, Qt::FindChildOptions options)
1091{
1092 Q_D(QLayout);
1093 if (!from || !to)
1094 return nullptr;
1095 if (from == to) // Do not return a QLayoutItem for \a from, since ownership still
1096 return nullptr; // belongs to the layout (since nothing was changed)
1097
1098 int index = -1;
1099 QLayoutItem *item = nullptr;
1100 for (int u = 0; u < count(); ++u) {
1101 item = itemAt(u);
1102 if (!item)
1103 continue;
1104
1105 if (item->widget() == from) {
1106 index = u;
1107 break;
1108 }
1109
1110 if (item->layout() && (options & Qt::FindChildrenRecursively)) {
1111 QLayoutItem *r = item->layout()->replaceWidget(from, to, options);
1112 if (r)
1113 return r;
1114 }
1115 }
1116 if (index == -1)
1117 return nullptr;
1118
1119 addChildWidget(to);
1120 QLayoutItem *newitem = new QWidgetItem(to);
1121 newitem->setAlignment(item->alignment());
1122 QLayoutItem *r = d->replaceAt(index, newitem);
1123 if (!r)
1124 delete newitem;
1125 return r;
1126}
1127
1178{
1179 const int c = count();
1180
1181 for (int i = 0; i < c; ++i) {
1182 if (itemAt(i)->widget() == widget)
1183 return i;
1184 }
1185
1186 return -1;
1187}
1188
1197{
1198 const int c = count();
1199
1200 for (int i = 0; i < c; ++i) {
1201 if (itemAt(i) == layoutItem)
1202 return i;
1203 }
1204
1205 return -1;
1206}
1207
1242{
1243 Q_D(QLayout);
1244 if (constraint == d->constraint)
1245 return;
1246
1247 d->constraint = constraint;
1248 invalidate();
1249}
1250
1252{
1253 Q_D(const QLayout);
1254 return d->constraint;
1255}
1256
1266{
1267 QSize s = sizeHint();
1268 Qt::Alignment a = alignment();
1269
1270 /*
1271 This is a hack to obtain the real maximum size, not
1272 QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX), the value consistently
1273 returned by QLayoutItems that have an alignment.
1274 */
1275 QLayout *that = const_cast<QLayout *>(this);
1276 that->setAlignment({ });
1277 QSize ms = that->maximumSize();
1278 that->setAlignment(a);
1279
1282 s.setWidth(qMin(r.width(), ms.width()));
1283 }
1286 s.setHeight(qMin(r.height(), ms.height()));
1287 } else if (hasHeightForWidth()) {
1288 int hfw = heightForWidth(s.width());
1289 if (hfw < s.height())
1290 s.setHeight(qMin(hfw, ms.height()));
1291 }
1292
1293 s = s.boundedTo(r.size());
1294 int x = r.x();
1295 int y = r.y();
1296
1297 if (a & Qt::AlignBottom)
1298 y += (r.height() - s.height());
1299 else if (!(a & Qt::AlignTop))
1300 y += (r.height() - s.height()) / 2;
1301
1304 if (a & Qt::AlignRight)
1305 x += (r.width() - s.width());
1306 else if (!(a & Qt::AlignLeft))
1307 x += (r.width() - s.width()) / 2;
1308
1309 return QRect(x, y, s.width(), s.height());
1310}
1311
1324{
1325 if (Q_UNLIKELY(!widget)) {
1326 qWarning("QLayout::removeWidget: Cannot remove a null widget.");
1327 return;
1328 }
1329
1330 int i = 0;
1332 while ((child = itemAt(i))) {
1333 if (child->widget() == widget) {
1334 delete takeAt(i);
1335 invalidate();
1336 } else {
1337 ++i;
1338 }
1339 }
1340}
1341
1352{
1353 int i = 0;
1355 while ((child = itemAt(i))) {
1356 if (child == item) {
1357 takeAt(i);
1358 invalidate();
1359 } else {
1360 ++i;
1361 }
1362 }
1363}
1364
1376{
1377 Q_D(QLayout);
1378 d->enabled = enable;
1379}
1380
1387{
1388 Q_D(const QLayout);
1389 return d->enabled;
1390}
1391
1399{
1401 result = result.expandedTo(qSmartMinSize(widget));
1402 QLayout *l = widget->layout();
1403 if (l && l->hasHeightForWidth() && result.height() < l->minimumHeightForWidth(result.width()) ) {
1404 QSize current = widget->size();
1405 int currentHfw = l->minimumHeightForWidth(current.width());
1406 int newHfw = l->minimumHeightForWidth(result.width());
1407 if (current.height() < currentHfw || currentHfw == newHfw) {
1408 //handle the constant hfw case and the vertical-only case, as well as the
1409 // current-size-is-not-correct case
1410 result.setHeight(newHfw);
1411 } else {
1412 // binary search; assume hfw is decreasing ###
1413
1414 int maxw = qMax(widget->width(),result.width());
1415 int maxh = qMax(widget->height(), result.height());
1416 int minw = qMin(widget->width(),result.width());
1417 int minh = qMin(widget->height(), result.height());
1418
1419 int minhfw = l->minimumHeightForWidth(minw);
1420 int maxhfw = l->minimumHeightForWidth(maxw);
1421 while (minw < maxw) {
1422 if (minhfw > maxh) { //assume decreasing
1423 minw = maxw - (maxw-minw)/2;
1424 minhfw = l->minimumHeightForWidth(minw);
1425 } else if (maxhfw < minh ) { //assume decreasing
1426 maxw = minw + (maxw-minw)/2;
1427 maxhfw = l->minimumHeightForWidth(maxw);
1428 } else {
1429 break;
1430 }
1431 }
1432 result = result.expandedTo(QSize(minw, minhfw));
1433 }
1434 }
1435 return result;
1436}
1437
1439
1440#include "moc_qlayout.cpp"
\inmodule QtCore
Definition qcoreevent.h:379
QObject * child() const
Returns the child object that was added or removed.
Definition qcoreevent.h:384
static void postEvent(QObject *receiver, QEvent *event, int priority=Qt::NormalEventPriority)
\inmodule QtCore
Definition qcoreevent.h:45
Type
This enum type defines the valid event types in Qt.
Definition qcoreevent.h:51
@ ChildRemoved
Definition qcoreevent.h:108
@ LayoutRequest
Definition qcoreevent.h:112
Type type() const
Returns the event type.
Definition qcoreevent.h:304
Qt::LayoutDirection layoutDirection
the default layout direction for this application
The QLayoutItem class provides an abstract item that a QLayout manipulates.
Definition qlayoutitem.h:25
virtual bool hasHeightForWidth() const
Returns true if this layout's preferred height depends on its width; otherwise returns false.
virtual int heightForWidth(int) const
Returns the preferred height for this layout item, given the width, which is not used in this default...
Qt::Alignment alignment() const
Returns the alignment of this item.
Definition qlayoutitem.h:45
virtual int minimumHeightForWidth(int) const
Returns the minimum height this widget needs for the given width, w.
virtual QWidget * widget() const
If this item manages a QWidget, returns that widget.
virtual QSize sizeHint() const =0
Implemented in subclasses to return the preferred size of this item.
virtual void invalidate()
Invalidates any cached information in this layout item.
bool checkLayout(QLayout *otherLayout) const
Returns true if the otherLayout can be added to the layout; otherwise returns false.
Definition qlayout.cpp:812
void getMargin(int *result, int userMargin, QStyle::PixelMetric pm) const
Definition qlayout.cpp:126
static QWidgetItem * createWidgetItem(const QLayout *layout, QWidget *widget)
Definition qlayout.cpp:148
bool checkWidget(QWidget *widget) const
Returns true if the widget can be added to the layout; otherwise returns false.
Definition qlayout.cpp:791
QWidgetItem *(* QWidgetItemFactoryMethod)(const QLayout *layout, QWidget *widget)
Definition qlayout_p.h:34
static QWidgetItemFactoryMethod widgetItemFactoryMethod
Definition qlayout_p.h:49
void reparentChildWidgets(QWidget *mw)
Definition qlayout.cpp:755
void doResize()
Definition qlayout.cpp:498
QWidget * menubar
Definition qlayout_p.h:63
static QSpacerItem * createSpacerItem(const QLayout *layout, int w, int h, QSizePolicy::Policy hPolicy=QSizePolicy::Minimum, QSizePolicy::Policy vPolicy=QSizePolicy::Minimum)
Definition qlayout.cpp:156
static QSpacerItemFactoryMethod spacerItemFactoryMethod
Definition qlayout_p.h:50
QSpacerItem *(* QSpacerItemFactoryMethod)(const QLayout *layout, int w, int h, QSizePolicy::Policy hPolicy, QSizePolicy::Policy)
Definition qlayout_p.h:35
The QLayout class is the base class of geometry managers.
Definition qlayout.h:26
QRect geometry() const override
\reimp
Definition qlayout.cpp:460
QSize totalSizeHint() const
Definition qlayout.cpp:644
int totalHeightForWidth(int w) const
Definition qlayout.cpp:599
void widgetEvent(QEvent *)
Definition qlayout.cpp:520
void removeWidget(QWidget *w)
Removes the widget widget from the layout.
Definition qlayout.cpp:1323
QSizePolicy::ControlTypes controlTypes() const override
\reimp
Definition qlayout.cpp:438
void addChildWidget(QWidget *w)
This function is called from addWidget() functions in subclasses to add w as a managed widget of a la...
Definition qlayout.cpp:837
void addWidget(QWidget *w)
Adds widget w to this layout in a manner specific to the layout.
Definition qlayout.cpp:186
int totalMinimumHeightForWidth(int w) const
Definition qlayout.cpp:577
void childEvent(QChildEvent *e) override
\reimp
Definition qlayout.cpp:560
void update()
Updates the layout for parentWidget().
Definition qlayout.cpp:971
bool isEmpty() const override
\reimp
Definition qlayout.cpp:422
bool isEnabled() const
Returns true if the layout is enabled; otherwise returns false.
Definition qlayout.cpp:1386
QRect alignmentRect(const QRect &) const
Returns the rectangle that should be covered when the geometry of this layout is set to r,...
Definition qlayout.cpp:1265
int spacing
the spacing between widgets inside the layout
Definition qlayout.h:30
QSize maximumSize() const override
Returns the maximum size of this layout.
Definition qlayout.cpp:927
void unsetContentsMargins()
Definition qlayout.cpp:326
QRect contentsRect() const
Definition qlayout.cpp:381
virtual void setSpacing(int)
Definition qlayout.cpp:266
SizeConstraint sizeConstraint
the resize mode of the layout
Definition qlayout.h:33
void setSizeConstraint(SizeConstraint)
Definition qlayout.cpp:1241
bool activate()
Redoes the layout for parentWidget() if necessary.
Definition qlayout.cpp:995
virtual void addItem(QLayoutItem *)=0
Implemented in subclasses to add an item.
QSize totalMaximumSize() const
Definition qlayout.cpp:669
QSize totalMinimumSize() const
Definition qlayout.cpp:621
SizeConstraint
The possible values are:
Definition qlayout.h:35
@ SetDefaultConstraint
Definition qlayout.h:36
@ SetMaximumSize
Definition qlayout.h:40
@ SetNoConstraint
Definition qlayout.h:37
@ SetFixedSize
Definition qlayout.h:39
@ SetMinimumSize
Definition qlayout.h:38
@ SetMinAndMaxSize
Definition qlayout.h:41
void getContentsMargins(int *left, int *top, int *right, int *bottom) const
Definition qlayout.cpp:347
virtual QLayoutItem * itemAt(int index) const =0
Must be implemented in subclasses to return the layout item at index.
static QSize closestAcceptableSize(const QWidget *w, const QSize &s)
Returns a size that satisfies all size constraints on widget, including heightForWidth() and that is ...
Definition qlayout.cpp:1398
virtual QLayoutItem * takeAt(int index)=0
Must be implemented in subclasses to remove the layout item at index from the layout,...
Qt::Orientations expandingDirections() const override
Returns whether this layout can make use of more space than sizeHint().
Definition qlayout.cpp:944
QLayout(QWidget *parent=nullptr)
Constructs a new top-level QLayout, with parent parent.
Definition qlayout.cpp:83
QMargins contentsMargins
Definition qlayout.h:32
QSize minimumSize() const override
Returns the minimum size of this layout.
Definition qlayout.cpp:912
bool setAlignment(QWidget *w, Qt::Alignment alignment)
Sets the alignment for widget w to alignment and returns true if w is found in this layout (not inclu...
Definition qlayout.cpp:199
void invalidate() override
\reimp
Definition qlayout.cpp:469
virtual QLayoutItem * replaceWidget(QWidget *from, QWidget *to, Qt::FindChildOptions options=Qt::FindChildrenRecursively)
Definition qlayout.cpp:1090
QWidget * menuBar() const
Returns the menu bar set for this layout, or \nullptr if no menu bar is set.
Definition qlayout.cpp:895
void setMenuBar(QWidget *w)
Tells the geometry manager to place the menu bar widget at the top of parentWidget(),...
Definition qlayout.cpp:882
QWidget * parentWidget() const
Returns the parent widget of this layout, or \nullptr if this layout is not installed on any widget.
Definition qlayout.cpp:399
void setEnabled(bool)
Enables this layout if enable is true, otherwise disables it.
Definition qlayout.cpp:1375
virtual int count() const =0
Must be implemented in subclasses to return the number of items in the layout.
bool adoptLayout(QLayout *layout)
Definition qlayout.cpp:737
virtual void setGeometry(const QRect &) override
\reimp
Definition qlayout.cpp:451
QLayout * layout() override
\reimp
void addChildLayout(QLayout *l)
This function is called from addLayout() or insertLayout() functions in subclasses to add layout chil...
Definition qlayout.cpp:719
void removeItem(QLayoutItem *)
Removes the layout item item from the layout.
Definition qlayout.cpp:1351
virtual int indexOf(const QWidget *) const
Searches for widget widget in this layout (not including child layouts).
Definition qlayout.cpp:1177
void setContentsMargins(int left, int top, int right, int bottom)
Definition qlayout.cpp:288
\inmodule QtCore
Definition qmargins.h:24
constexpr int bottom() const noexcept
Returns the bottom margin.
Definition qmargins.h:115
constexpr int left() const noexcept
Returns the left margin.
Definition qmargins.h:106
constexpr int right() const noexcept
Returns the right margin.
Definition qmargins.h:112
constexpr int top() const noexcept
Returns the top margin.
Definition qmargins.h:109
uint wasWidget
Definition qobject.h:86
static QObjectPrivate * get(QObject *o)
Definition qobject_p.h:150
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
QString objectName
the name of this object
Definition qobject.h:107
void setParent(QObject *parent)
Makes the object a child of parent.
Definition qobject.cpp:2195
bool isWidgetType() const
Returns true if the object is a widget; otherwise returns false.
Definition qobject.h:131
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int top() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:176
constexpr int left() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:173
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:236
constexpr void setTop(int pos) noexcept
Sets the top edge of the rectangle to the given y coordinate.
Definition qrect.h:194
Policy
This enum describes the various per-dimension sizing types used when constructing a QSizePolicy.
Definition qsizepolicy.h:29
\inmodule QtCore
Definition qsize.h:25
constexpr QSize boundedTo(const QSize &) const noexcept
Returns a size holding the minimum width and height of this size and the given otherSize.
Definition qsize.h:197
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
constexpr void setWidth(int w) noexcept
Sets the width to the given width.
Definition qsize.h:136
constexpr void setHeight(int h) noexcept
Sets the height to the given height.
Definition qsize.h:139
The QSpacerItem class provides blank space in a layout.
Definition qlayoutitem.h:57
static Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
Transforms an alignment of Qt::AlignLeft or Qt::AlignRight without Qt::AlignAbsolute into Qt::AlignLe...
Definition qstyle.cpp:2202
PixelMetric
This enum describes the various available pixel metrics.
Definition qstyle.h:413
@ PM_LayoutBottomMargin
Definition qstyle.h:515
@ PM_LayoutLeftMargin
Definition qstyle.h:512
@ PM_LayoutHorizontalSpacing
Definition qstyle.h:516
@ PM_LayoutTopMargin
Definition qstyle.h:513
@ PM_LayoutRightMargin
Definition qstyle.h:514
The QWidgetItem class is a layout item that represents a widget.
Definition qlayoutitem.h:86
static QWidgetPrivate * get(QWidget *w)
Definition qwidget_p.h:212
short bottommargin
Definition qwidget_p.h:696
std::unique_ptr< QWExtra > extra
Definition qwidget_p.h:647
short rightmargin
Definition qwidget_p.h:695
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:886
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
bool isHidden() const
Returns true if the widget is hidden, otherwise returns false.
Definition qwidget.h:877
QLayout * layout() const
Returns the layout manager that is installed on this widget, or \nullptr if no layout manager is inst...
QSize maximumSize
the widget's maximum size in pixels
Definition qwidget.h:121
int minimumWidth
the widget's minimum width in pixels
Definition qwidget.h:123
QSize sizeHint
the recommended size for the widget
Definition qwidget.h:148
QWidget * parentWidget() const
Returns the parent of this widget, or \nullptr if it does not have any parent widget.
Definition qwidget.h:904
bool isWindow() const
Returns true if the widget is an independent window, otherwise returns false.
Definition qwidget.h:811
virtual int heightForWidth(int) const
Returns the preferred height for this widget, given the width w.
QOpenGLWidget * widget
[1]
qreal spacing
uint alignment
Combined button and popup list for selecting options.
@ AlignRight
Definition qnamespace.h:146
@ AlignBottom
Definition qnamespace.h:154
@ AlignTop
Definition qnamespace.h:153
@ AlignHorizontal_Mask
Definition qnamespace.h:151
@ AlignVertical_Mask
Definition qnamespace.h:161
@ AlignLeft
Definition qnamespace.h:144
@ WA_LaidOut
Definition qnamespace.h:289
@ WA_LayoutOnEntireRect
Definition qnamespace.h:314
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
@ FindChildrenRecursively
@ QueuedConnection
#define Q_UNLIKELY(x)
#define QT_RETHROW
#define QT_CATCH(A)
#define QT_TRY
static QT_BEGIN_NAMESPACE int menuBarHeightForWidth(QWidget *menubar, int w)
Definition qlayout.cpp:25
static bool removeWidgetRecursively(QLayoutItem *li, QObject *w)
Definition qlayout.cpp:476
Q_WIDGETS_EXPORT QSize qSmartMinSize(const QSize &sizeHint, const QSize &minSizeHint, const QSize &minSize, const QSize &maxSize, const QSizePolicy &sizePolicy)
Q_WIDGETS_EXPORT int qSmartSpacing(const QLayout *layout, QStyle::PixelMetric pm)
Q_WIDGETS_EXPORT QSize qSmartMaxSize(const QSize &sizeHint, const QSize &minSize, const QSize &maxSize, const QSizePolicy &sizePolicy, Qt::Alignment align)
QT_BEGIN_NAMESPACE constexpr int QLAYOUTSIZE_MAX
Definition qlayoutitem.h:16
#define qWarning
Definition qlogging.h:166
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLboolean r
[2]
GLsizei GLenum GLenum * types
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLint left
GLenum type
GLint GLint bottom
GLboolean enable
GLfloat n
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
static void layoutItem(QQuickItem *item, qreal y, qreal width)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define qUtf16Printable(string)
Definition qstring.h:1543
Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nullptr) noexcept
unsigned int uint
Definition qtypes.h:34
const char className[16]
[1]
Definition qwizard.cpp:100
obj metaObject() -> className()
QObject::connect nullptr
QVBoxLayout * layout
QGraphicsItem * item
edit isVisible()
QLayoutItem * child
[0]
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(nullptr), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
\threadsafe This is an overloaded member function, provided for convenience. It differs from the abov...