1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31#include <qlayout.h>
32#include <qapplication.h>
33#include <qwidget.h>
34#include <qproxystyle.h>
35#include <qsizepolicy.h>
36
37#include <QtWidgets/QCheckBox>
38#include <QtWidgets/QLabel>
39#include <QtWidgets/QLineEdit>
40#include <QtWidgets/QPushButton>
41
42#include <private/qdialog_p.h>
43
44#include <QStyleFactory>
45#include <QSharedPointer>
46
47#include <QtTest/private/qtesthelpers_p.h>
48
49using namespace QTestPrivate;
50
51#include <qformlayout.h>
52
53// ItemRole has enumerators for numerical values 0..2, thus the only
54// valid numerical values for storing into an ItemRole variable are 0..3:
55Q_CONSTEXPR QFormLayout::ItemRole invalidRole = QFormLayout::ItemRole(3);
56
57struct QFormLayoutTakeRowResultHolder {
58 QFormLayoutTakeRowResultHolder(QFormLayout::TakeRowResult result) noexcept
59 : labelItem(result.labelItem),
60 fieldItem(result.fieldItem)
61 {
62 }
63 ~QFormLayoutTakeRowResultHolder()
64 {
65 // re-use a QFormLayout to recursively reap the QLayoutItems:
66 QFormLayout disposer;
67 if (labelItem)
68 disposer.setItem(row: 0, role: QFormLayout::LabelRole, item: labelItem);
69 if (fieldItem)
70 disposer.setItem(row: 0, role: QFormLayout::FieldRole, item: fieldItem);
71 }
72 QFormLayoutTakeRowResultHolder(QFormLayoutTakeRowResultHolder &&other) noexcept
73 : labelItem(other.labelItem),
74 fieldItem(other.fieldItem)
75 {
76 other.labelItem = nullptr;
77 other.fieldItem = nullptr;
78 }
79 QFormLayoutTakeRowResultHolder &operator=(QFormLayoutTakeRowResultHolder &&other) noexcept
80 {
81 swap(other);
82 return *this;
83 }
84
85 void swap(QFormLayoutTakeRowResultHolder &other) noexcept
86 {
87 qSwap(value1&: labelItem, value2&: other.labelItem);
88 qSwap(value1&: fieldItem, value2&: other.fieldItem);
89 }
90
91 QLayoutItem *labelItem;
92 QLayoutItem *fieldItem;
93};
94
95class tst_QFormLayout : public QObject
96{
97 Q_OBJECT
98
99private slots:
100 void cleanup();
101 void rowCount();
102 void buddies();
103 void getItemPosition();
104 void wrapping();
105 void spacing();
106 void contentsRect();
107
108 void setFormStyle();
109 void setFieldGrowthPolicy();
110 void setRowWrapPolicy();
111 void setLabelAlignment();
112 void setFormAlignment();
113
114/*
115 void setHorizontalSpacing(int spacing);
116 int horizontalSpacing() const;
117 void setVerticalSpacing(int spacing);
118 int verticalSpacing() const;
119*/
120
121 void addRow();
122 void insertRow_QWidget_QWidget();
123 void insertRow_QWidget_QLayout();
124 void insertRow_QString_QWidget();
125 void insertRow_QString_QLayout();
126 void insertRow_QWidget();
127 void insertRow_QLayout();
128 void removeRow();
129 void removeRow_QWidget();
130 void removeRow_QLayout();
131 void takeRow();
132 void takeRow_QWidget();
133 void takeRow_QLayout();
134 void setWidget();
135 void setLayout();
136
137/*
138 QLayoutItem *itemAt(int row, ItemRole role) const;
139 void getItemPosition(int index, int *rowPtr, ItemRole *rolePtr) const;
140 void getLayoutPosition(QWidget *widget, int *rowPtr, ItemRole *rolePtr) const;
141 void getItemPosition(QLayoutItem *item, int *rowPtr, ItemRole *rolePtr) const;
142 QWidget *labelForField(QWidget *widget) const;
143 QWidget *labelForField(QLayoutItem *item) const;
144
145 void addItem(QLayoutItem *item);
146*/
147
148 void itemAt();
149 void takeAt();
150 void layoutAlone();
151 void replaceWidget();
152/*
153 void setGeometry(const QRect &rect);
154 QSize minimumSize() const;
155 QSize sizeHint() const;
156
157 bool hasHeightForWidth() const;
158 int heightForWidth(int width) const;
159 Qt::Orientations expandingDirections() const;
160*/
161
162 void taskQTBUG_27420_takeAtShouldUnparentLayout();
163 void taskQTBUG_40609_addingWidgetToItsOwnLayout();
164 void taskQTBUG_40609_addingLayoutToItself();
165
166};
167
168void tst_QFormLayout::cleanup()
169{
170 QTRY_VERIFY(QApplication::topLevelWidgets().isEmpty());
171}
172
173void tst_QFormLayout::rowCount()
174{
175 QWidget w;
176 QFormLayout *fl = new QFormLayout(&w);
177
178 fl->addRow(labelText: tr(s: "Label 1"), field: new QLineEdit);
179 fl->addRow(labelText: tr(s: "Label 2"), field: new QLineEdit);
180 fl->addRow(labelText: tr(s: "Label 3"), field: new QLineEdit);
181 QCOMPARE(fl->rowCount(), 3);
182
183 fl->addRow(widget: new QWidget);
184 fl->addRow(layout: new QHBoxLayout);
185 QCOMPARE(fl->rowCount(), 5);
186
187 fl->insertRow(row: 1, labelText: tr(s: "Label 0.5"), field: new QLineEdit);
188 QCOMPARE(fl->rowCount(), 6);
189
190 //TODO: remove items
191}
192
193void tst_QFormLayout::buddies()
194{
195 QWidget w;
196 QFormLayout *fl = new QFormLayout(&w);
197
198 //normal buddy case
199 QLineEdit *le = new QLineEdit;
200 fl->addRow(labelText: tr(s: "Label 1"), field: le);
201 QLabel *label = qobject_cast<QLabel *>(object: fl->labelForField(field: le));
202 QVERIFY(label);
203 QWidget *lew = le;
204 QCOMPARE(label->buddy(), lew);
205
206 //null label
207 QLineEdit *le2 = new QLineEdit;
208 fl->addRow(label: 0, field: le2);
209 QWidget *label2 = fl->labelForField(field: le2);
210 QVERIFY(!label2);
211
212 //no label
213 QLineEdit *le3 = new QLineEdit;
214 fl->addRow(widget: le3);
215 QWidget *label3 = fl->labelForField(field: le3);
216 QVERIFY(!label3);
217
218 //TODO: empty label?
219}
220
221void tst_QFormLayout::getItemPosition()
222{
223 QWidget w;
224 QFormLayout *fl = new QFormLayout(&w);
225
226 QList<QLabel*> labels;
227 QList<QLineEdit*> fields;
228 for (int i = 0; i < 5; ++i) {
229 labels.append(t: new QLabel(QLatin1String("Label " ) + QString::number(i + 1)));
230 fields.append(t: new QLineEdit);
231 fl->addRow(label: labels[i], field: fields[i]);
232 }
233
234 //a field
235 {
236 int row;
237 QFormLayout::ItemRole role;
238 fl->getWidgetPosition(widget: fields[3], rowPtr: &row, rolePtr: &role);
239 QCOMPARE(row, 3);
240 QCOMPARE(role, QFormLayout::FieldRole);
241 }
242
243 //a label
244 {
245 int row;
246 QFormLayout::ItemRole role;
247 fl->getWidgetPosition(widget: labels[2], rowPtr: &row, rolePtr: &role);
248 QCOMPARE(row, 2);
249 QCOMPARE(role, QFormLayout::LabelRole);
250 }
251
252 //a layout that's been inserted
253 {
254 QVBoxLayout *vbl = new QVBoxLayout;
255 fl->insertRow(row: 2, labelText: "Label 1.5", field: vbl);
256 int row;
257 QFormLayout::ItemRole role;
258 fl->getLayoutPosition(layout: vbl, rowPtr: &row, rolePtr: &role);
259 QCOMPARE(row, 2);
260 QCOMPARE(role, QFormLayout::FieldRole);
261 }
262}
263
264void tst_QFormLayout::wrapping()
265{
266 QWidget w;
267 QFormLayout *fl = new QFormLayout(&w);
268 fl->setRowWrapPolicy(QFormLayout::WrapLongRows);
269
270 QLineEdit *le = new QLineEdit;
271 QLabel *lbl = new QLabel("A long label");
272 le->setMinimumWidth(200);
273 fl->addRow(label: lbl, field: le);
274
275 w.setFixedWidth(240);
276 w.setWindowTitle(QTest::currentTestFunction());
277 w.show();
278
279#ifdef Q_OS_WINRT
280 QEXPECT_FAIL("", "setFixedWidth does not work on WinRT", Abort);
281#endif
282 QCOMPARE(le->geometry().y() > lbl->geometry().y(), true);
283
284 //TODO: additional tests covering different wrapping cases
285}
286
287class CustomLayoutStyle : public QProxyStyle
288{
289 Q_OBJECT
290public:
291 CustomLayoutStyle() : QProxyStyle(QStyleFactory::create("windows"))
292 {
293 hspacing = 5;
294 vspacing = 10;
295 }
296
297 virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0,
298 const QWidget * widget = 0 ) const;
299
300 int hspacing;
301 int vspacing;
302};
303
304int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/,
305 const QWidget * widget /*= 0*/ ) const
306{
307 switch (metric) {
308 case PM_LayoutHorizontalSpacing:
309 return hspacing;
310 case PM_LayoutVerticalSpacing:
311 return vspacing;
312 break;
313 default:
314 break;
315 }
316 return QProxyStyle::pixelMetric(metric, option, widget);
317}
318
319void tst_QFormLayout::spacing()
320{
321 //TODO: confirm spacing behavior
322 QWidget w;
323 QScopedPointer<CustomLayoutStyle> style(new CustomLayoutStyle);
324 style->hspacing = 5;
325 style->vspacing = 10;
326 w.setStyle(style.data());
327 QFormLayout *fl = new QFormLayout(&w);
328 QCOMPARE(style->hspacing, fl->horizontalSpacing());
329 QCOMPARE(style->vspacing, fl->verticalSpacing());
330
331 //QCOMPARE(fl->spacing(), -1);
332 fl->setVerticalSpacing(5);
333 QCOMPARE(5, fl->horizontalSpacing());
334 QCOMPARE(5, fl->verticalSpacing());
335 //QCOMPARE(fl->spacing(), 5);
336 fl->setVerticalSpacing(-1);
337 QCOMPARE(style->hspacing, fl->horizontalSpacing());
338 QCOMPARE(style->vspacing, fl->verticalSpacing());
339
340 style->hspacing = 5;
341 style->vspacing = 5;
342 //QCOMPARE(fl->spacing(), 5);
343
344 fl->setHorizontalSpacing(20);
345 //QCOMPARE(fl->spacing(), -1);
346 style->vspacing = 20;
347 QCOMPARE(fl->horizontalSpacing(), 20);
348 QCOMPARE(fl->verticalSpacing(), 20);
349 //QCOMPARE(fl->spacing(), 20);
350 fl->setHorizontalSpacing(-1);
351 //QCOMPARE(fl->spacing(), -1);
352 style->hspacing = 20;
353 //QCOMPARE(fl->spacing(), 20);
354
355
356
357 // Do not assert if spacings are negative (QTBUG-34731)
358 style->vspacing = -1;
359 style->hspacing = -1;
360 QLabel *label = new QLabel(tr(s: "Asserts"));
361 QCheckBox *checkBox = new QCheckBox(tr(s: "Yes"));
362 fl->setWidget(row: 0, role: QFormLayout::LabelRole, widget: label);
363 fl->setWidget(row: 1, role: QFormLayout::FieldRole, widget: checkBox);
364 w.resize(w: 200, h: 100);
365 w.setWindowTitle(QTest::currentTestFunction());
366 w.show();
367 QVERIFY(QTest::qWaitForWindowExposed(&w));
368}
369
370void tst_QFormLayout::contentsRect()
371{
372 QWidget w;
373 setFrameless(&w);
374 QFormLayout form;
375 w.setLayout(&form);
376 form.addRow(labelText: "Label", field: new QPushButton(&w));
377 w.setWindowTitle(QTest::currentTestFunction());
378 w.show();
379 QVERIFY(QTest::qWaitForWindowExposed(&w));
380 int l, t, r, b;
381 form.getContentsMargins(left: &l, top: &t, right: &r, bottom: &b);
382 QRect geom = form.geometry();
383
384 QCOMPARE(geom.adjusted(+l, +t, -r, -b), form.contentsRect());
385}
386
387
388class DummyMacStyle : public QCommonStyle
389{
390public:
391 virtual int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const
392 {
393 switch(hint) {
394 case SH_FormLayoutFormAlignment:
395 return Qt::AlignHCenter | Qt::AlignTop;
396 case SH_FormLayoutLabelAlignment:
397 return Qt::AlignRight;
398 case SH_FormLayoutWrapPolicy:
399 return QFormLayout::DontWrapRows;
400 case SH_FormLayoutFieldGrowthPolicy:
401 return QFormLayout::FieldsStayAtSizeHint;
402 default:
403 return QCommonStyle::styleHint(sh: hint, opt: option, w: widget, shret: returnData);
404 }
405 }
406};
407
408class DummyQtopiaStyle : public QCommonStyle
409{
410public:
411 virtual int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const
412 {
413 switch(hint) {
414 case SH_FormLayoutFormAlignment:
415 return Qt::AlignLeft | Qt::AlignTop;
416 case SH_FormLayoutLabelAlignment:
417 return Qt::AlignRight;
418 case SH_FormLayoutWrapPolicy:
419 return QFormLayout::WrapLongRows;
420 case SH_FormLayoutFieldGrowthPolicy:
421 return QFormLayout::AllNonFixedFieldsGrow;
422 default:
423 return QCommonStyle::styleHint(sh: hint, opt: option, w: widget, shret: returnData);
424 }
425 }
426};
427
428void tst_QFormLayout::setFormStyle()
429{
430 QWidget widget;
431 QFormLayout layout;
432 widget.setLayout(&layout);
433
434#if 0 // QT_NO_STYLE_PLASTIQUE
435 widget.setStyle(new QPlastiqueStyle());
436
437 QCOMPARE(layout.labelAlignment(), Qt::AlignRight);
438 QVERIFY(layout.formAlignment() == (Qt::AlignLeft | Qt::AlignTop));
439 QCOMPARE(layout.fieldGrowthPolicy(), QFormLayout::ExpandingFieldsGrow);
440 QCOMPARE(layout.rowWrapPolicy(), QFormLayout::DontWrapRows);
441#endif
442
443 const QScopedPointer<QStyle> windowsStyle(QStyleFactory::create("windows"));
444 widget.setStyle(windowsStyle.data());
445
446 QCOMPARE(layout.labelAlignment(), Qt::AlignLeft);
447 QVERIFY(layout.formAlignment() == (Qt::AlignLeft | Qt::AlignTop));
448 QCOMPARE(layout.fieldGrowthPolicy(), QFormLayout::AllNonFixedFieldsGrow);
449 QCOMPARE(layout.rowWrapPolicy(), QFormLayout::DontWrapRows);
450
451 /* can't directly create mac style or qtopia style, since
452 this test is cross platform.. so create dummy styles that
453 return all the right stylehints.
454 */
455 DummyMacStyle macStyle;
456 widget.setStyle(&macStyle);
457
458 QCOMPARE(layout.labelAlignment(), Qt::AlignRight);
459 QVERIFY(layout.formAlignment() == (Qt::AlignHCenter | Qt::AlignTop));
460 QCOMPARE(layout.fieldGrowthPolicy(), QFormLayout::FieldsStayAtSizeHint);
461 QCOMPARE(layout.rowWrapPolicy(), QFormLayout::DontWrapRows);
462
463 DummyQtopiaStyle qtopiaStyle;
464 widget.setStyle(&qtopiaStyle);
465
466 QCOMPARE(layout.labelAlignment(), Qt::AlignRight);
467 QVERIFY(layout.formAlignment() == (Qt::AlignLeft | Qt::AlignTop));
468 QCOMPARE(layout.fieldGrowthPolicy(), QFormLayout::AllNonFixedFieldsGrow);
469 QCOMPARE(layout.rowWrapPolicy(), QFormLayout::WrapLongRows);
470}
471
472void tst_QFormLayout::setFieldGrowthPolicy()
473{
474 QWidget window;
475 QLineEdit fld1, fld2, fld3;
476 fld1.setSizePolicy(hor: QSizePolicy::Fixed, ver: QSizePolicy::Fixed);
477 fld2.setSizePolicy(hor: QSizePolicy::Preferred, ver: QSizePolicy::Fixed);
478 fld3.setSizePolicy(hor: QSizePolicy::Expanding, ver: QSizePolicy::Fixed);
479
480 QFormLayout layout;
481 layout.addRow(labelText: "One:", field: &fld1);
482 layout.addRow(labelText: "Two:", field: &fld2);
483 layout.addRow(labelText: "Three:", field: &fld3);
484 window.setLayout(&layout);
485 window.resize(w: 1000, h: 200);
486
487 for (int i = 0; i < 3; ++i) {
488 layout.setFieldGrowthPolicy(i == 0 ? QFormLayout::FieldsStayAtSizeHint :
489 i == 1 ? QFormLayout::ExpandingFieldsGrow :
490 QFormLayout::AllNonFixedFieldsGrow);
491 layout.activate();
492
493 if (i == 0) {
494 QCOMPARE(fld1.width(), fld2.width());
495 QCOMPARE(fld2.width(), fld3.width());
496 } else if (i == 1) {
497 QCOMPARE(fld1.width(), fld2.width());
498 QVERIFY(fld2.width() < fld3.width());
499 } else {
500 QVERIFY(fld1.width() < fld2.width());
501 QCOMPARE(fld2.width(), fld3.width());
502 }
503 }
504}
505
506void tst_QFormLayout::setRowWrapPolicy()
507{
508}
509
510void tst_QFormLayout::setLabelAlignment()
511{
512}
513
514void tst_QFormLayout::setFormAlignment()
515{
516}
517
518void tst_QFormLayout::addRow()
519{
520 QWidget topLevel;
521 QFormLayout *layout = new QFormLayout(&topLevel);
522 QWidget *w1 = new QWidget(&topLevel);
523 QWidget *w2 = new QWidget(&topLevel);
524 QWidget *w3 = new QWidget(&topLevel);
525 QHBoxLayout *l1 = new QHBoxLayout;
526 QHBoxLayout *l2 = new QHBoxLayout;
527 QHBoxLayout *l3 = new QHBoxLayout;
528 QLabel *lbl1 = new QLabel(&topLevel);
529 QLabel *lbl2 = new QLabel(&topLevel);
530
531 QCOMPARE(layout->rowCount(), 0);
532
533 layout->addRow(label: lbl1, field: w1);
534 layout->addRow(label: lbl2, field: l1);
535 layout->addRow(labelText: "Foo:", field: w2);
536 layout->addRow(labelText: "Bar:", field: l2);
537 layout->addRow(widget: w3);
538 layout->addRow(layout: l3);
539
540 QCOMPARE(layout->rowCount(), 6);
541
542 QVERIFY(layout->itemAt(0, QFormLayout::LabelRole)->widget() == lbl1);
543 QVERIFY(layout->itemAt(1, QFormLayout::LabelRole)->widget() == lbl2);
544 QCOMPARE(layout->itemAt(2, QFormLayout::LabelRole)->widget()->property("text").toString(), QLatin1String("Foo:"));
545 QCOMPARE(layout->itemAt(3, QFormLayout::LabelRole)->widget()->property("text").toString(), QLatin1String("Bar:"));
546 QVERIFY(layout->itemAt(4, QFormLayout::LabelRole) == 0);
547 QVERIFY(layout->itemAt(5, QFormLayout::LabelRole) == 0);
548
549 QVERIFY(layout->itemAt(0, QFormLayout::FieldRole)->widget() == w1);
550 QVERIFY(layout->itemAt(1, QFormLayout::FieldRole)->layout() == l1);
551 QVERIFY(layout->itemAt(2, QFormLayout::FieldRole)->widget() == w2);
552 QVERIFY(layout->itemAt(3, QFormLayout::FieldRole)->layout() == l2);
553// ### should have a third role, FullRowRole?
554// QVERIFY(layout.itemAt(4, QFormLayout::FieldRole) == 0);
555// QVERIFY(layout.itemAt(5, QFormLayout::FieldRole) == 0);
556}
557
558void tst_QFormLayout::insertRow_QWidget_QWidget()
559{
560 QWidget topLevel;
561 QFormLayout *layout = new QFormLayout(&topLevel);
562 QLabel *lbl1 = new QLabel(&topLevel);
563 QLabel *lbl2 = new QLabel(&topLevel);
564 QLabel *lbl3 = new QLabel(&topLevel);
565 QLabel *lbl4 = new QLabel(&topLevel);
566 QLineEdit *fld1 = new QLineEdit(&topLevel);
567 QLineEdit *fld2 = new QLineEdit(&topLevel);
568 QLineEdit *fld3 = new QLineEdit(&topLevel);
569 QLineEdit *fld4 = new QLineEdit(&topLevel);
570
571 layout->insertRow(row: 0, label: lbl1, field: fld1);
572 QCOMPARE(layout->rowCount(), 1);
573
574 {
575 int row = -1;
576 QFormLayout::ItemRole role = invalidRole;
577 layout->getWidgetPosition(widget: lbl1, rowPtr: &row, rolePtr: &role);
578 QCOMPARE(row, 0);
579 QCOMPARE(int(role), int(QFormLayout::LabelRole));
580 }
581
582 {
583 int row = -1;
584 QFormLayout::ItemRole role = invalidRole;
585 layout->getWidgetPosition(widget: fld1, rowPtr: &row, rolePtr: &role);
586 QCOMPARE(row, 0);
587 QCOMPARE(int(role), int(QFormLayout::FieldRole));
588 }
589
590 // check that negative values append
591 layout->insertRow(row: -2, label: lbl2, field: fld2);
592 QCOMPARE(layout->rowCount(), 2);
593
594 QVERIFY(layout->itemAt(0, QFormLayout::LabelRole)->widget() == lbl1);
595 QVERIFY(layout->itemAt(1, QFormLayout::LabelRole)->widget() == lbl2);
596
597 // check that too large values append
598 layout->insertRow(row: 100, label: lbl3, field: fld3);
599 QCOMPARE(layout->rowCount(), 3);
600 QCOMPARE(layout->count(), 6);
601
602 layout->insertRow(row: 3, label: (QWidget *)0, field: (QWidget *)0);
603 QCOMPARE(layout->rowCount(), 4);
604 QCOMPARE(layout->count(), 6);
605
606 layout->insertRow(row: 4, label: (QWidget *)0, field: fld4);
607 QCOMPARE(layout->rowCount(), 5);
608 QCOMPARE(layout->count(), 7);
609
610 layout->insertRow(row: 5, label: lbl4, field: (QWidget *)0);
611 QCOMPARE(layout->rowCount(), 6);
612 QCOMPARE(layout->count(), 8);
613
614 QVERIFY(layout->itemAt(0, QFormLayout::LabelRole)->widget() == lbl1);
615 QVERIFY(layout->itemAt(1, QFormLayout::LabelRole)->widget() == lbl2);
616 QVERIFY(layout->itemAt(2, QFormLayout::LabelRole)->widget() == lbl3);
617 QVERIFY(layout->itemAt(3, QFormLayout::LabelRole) == 0);
618 QVERIFY(layout->itemAt(4, QFormLayout::LabelRole) == 0);
619 QVERIFY(layout->itemAt(5, QFormLayout::LabelRole)->widget() == lbl4);
620
621 QVERIFY(layout->itemAt(0, QFormLayout::FieldRole)->widget() == fld1);
622 QVERIFY(layout->itemAt(1, QFormLayout::FieldRole)->widget() == fld2);
623 QVERIFY(layout->itemAt(2, QFormLayout::FieldRole)->widget() == fld3);
624 QVERIFY(layout->itemAt(3, QFormLayout::FieldRole) == 0);
625 QVERIFY(layout->itemAt(4, QFormLayout::FieldRole)->widget() == fld4);
626 QVERIFY(layout->itemAt(5, QFormLayout::FieldRole) == 0);
627}
628
629void tst_QFormLayout::insertRow_QWidget_QLayout()
630{
631 QWidget topLevel;
632 QFormLayout *layout = new QFormLayout(&topLevel);
633 QLabel *lbl1 = new QLabel(&topLevel);
634 QLabel *lbl2 = new QLabel(&topLevel);
635 QLabel *lbl3 = new QLabel(&topLevel);
636 QHBoxLayout *fld1 = new QHBoxLayout;
637 QHBoxLayout *fld2 = new QHBoxLayout;
638 QHBoxLayout *fld3 = new QHBoxLayout;
639
640 layout->insertRow(row: 0, label: lbl1, field: fld1);
641 QCOMPARE(layout->rowCount(), 1);
642
643 {
644 int row = -1;
645 QFormLayout::ItemRole role = invalidRole;
646 layout->getWidgetPosition(widget: lbl1, rowPtr: &row, rolePtr: &role);
647 QCOMPARE(row, 0);
648 QCOMPARE(int(role), int(QFormLayout::LabelRole));
649 }
650
651 {
652 int row = -1;
653 QFormLayout::ItemRole role = invalidRole;
654 layout->getLayoutPosition(layout: fld1, rowPtr: &row, rolePtr: &role);
655 QCOMPARE(row, 0);
656 QCOMPARE(int(role), int(QFormLayout::FieldRole));
657 }
658
659 // check that negative values append
660 layout->insertRow(row: -2, label: lbl2, field: fld2);
661 QCOMPARE(layout->rowCount(), 2);
662
663 QVERIFY(layout->itemAt(0, QFormLayout::LabelRole)->widget() == lbl1);
664 QVERIFY(layout->itemAt(1, QFormLayout::LabelRole)->widget() == lbl2);
665
666 // check that too large values append
667 layout->insertRow(row: 100, label: lbl3, field: fld3);
668 QCOMPARE(layout->rowCount(), 3);
669
670 QVERIFY(layout->itemAt(0, QFormLayout::LabelRole)->widget() == lbl1);
671 QVERIFY(layout->itemAt(1, QFormLayout::LabelRole)->widget() == lbl2);
672 QVERIFY(layout->itemAt(2, QFormLayout::LabelRole)->widget() == lbl3);
673
674 QVERIFY(layout->itemAt(0, QFormLayout::FieldRole)->layout() == fld1);
675 QVERIFY(layout->itemAt(1, QFormLayout::FieldRole)->layout() == fld2);
676 QVERIFY(layout->itemAt(2, QFormLayout::FieldRole)->layout() == fld3);
677}
678
679void tst_QFormLayout::insertRow_QString_QWidget()
680{
681 QWidget topLevel;
682 QFormLayout *layout = new QFormLayout(&topLevel);
683 QLineEdit *fld1 = new QLineEdit(&topLevel);
684 QLineEdit *fld2 = new QLineEdit(&topLevel);
685 QLineEdit *fld3 = new QLineEdit(&topLevel);
686
687 layout->insertRow(row: -5, labelText: "&Name:", field: fld1);
688 QLabel *label1 = qobject_cast<QLabel *>(object: layout->itemAt(row: 0, role: QFormLayout::LabelRole)->widget());
689 QVERIFY(label1 != 0);
690 QCOMPARE(label1->buddy(), fld1);
691
692 layout->insertRow(row: 0, labelText: "&Email:", field: fld2);
693 QLabel *label2 = qobject_cast<QLabel *>(object: layout->itemAt(row: 0, role: QFormLayout::LabelRole)->widget());
694 QVERIFY(label2 != 0);
695 QCOMPARE(label2->buddy(), fld2);
696
697 layout->insertRow(row: 5, labelText: "&Age:", field: fld3);
698 QLabel *label3 = qobject_cast<QLabel *>(object: layout->itemAt(row: 2, role: QFormLayout::LabelRole)->widget());
699 QVERIFY(label3 != 0);
700 QCOMPARE(label3->buddy(), fld3);
701}
702
703void tst_QFormLayout::insertRow_QString_QLayout()
704{
705 QWidget topLevel;
706 QFormLayout *layout = new QFormLayout(&topLevel);
707 QHBoxLayout *fld1 = new QHBoxLayout;
708 QHBoxLayout *fld2 = new QHBoxLayout;
709 QHBoxLayout *fld3 = new QHBoxLayout;
710
711 layout->insertRow(row: -5, labelText: "&Name:", field: fld1);
712 QLabel *label1 = qobject_cast<QLabel *>(object: layout->itemAt(row: 0, role: QFormLayout::LabelRole)->widget());
713 QVERIFY(label1 != 0);
714 QVERIFY(!label1->buddy());
715
716 QCOMPARE(layout->rowCount(), 1);
717
718 layout->insertRow(row: 0, labelText: "&Email:", field: fld2);
719 QLabel *label2 = qobject_cast<QLabel *>(object: layout->itemAt(row: 0, role: QFormLayout::LabelRole)->widget());
720 QVERIFY(label2 != 0);
721 QVERIFY(!label2->buddy());
722
723 QCOMPARE(layout->rowCount(), 2);
724
725 layout->insertRow(row: 5, labelText: "&Age:", field: fld3);
726 QLabel *label3 = qobject_cast<QLabel *>(object: layout->itemAt(row: 2, role: QFormLayout::LabelRole)->widget());
727 QVERIFY(label3 != 0);
728 QVERIFY(!label3->buddy());
729
730 QCOMPARE(layout->rowCount(), 3);
731}
732
733void tst_QFormLayout::insertRow_QWidget()
734{
735 // ### come back to this later
736}
737
738void tst_QFormLayout::insertRow_QLayout()
739{
740 // ### come back to this later
741}
742
743void tst_QFormLayout::removeRow()
744{
745 QWidget topLevel;
746 QFormLayout *layout = new QFormLayout(&topLevel);
747
748 QCOMPARE(layout->count(), 0);
749 QCOMPARE(layout->rowCount(), 0);
750
751 QPointer<QWidget> w1 = new QWidget;
752 QPointer<QWidget> w2 = new QWidget;
753
754 layout->addRow(labelText: "test1", field: w1);
755 layout->addRow(widget: w2);
756
757 QCOMPARE(layout->count(), 3);
758 QCOMPARE(layout->rowCount(), 2);
759
760 layout->removeRow(row: 1);
761
762 QVERIFY(w1);
763 QVERIFY(!w2);
764 QCOMPARE(layout->count(), 2);
765 QCOMPARE(layout->rowCount(), 1);
766
767 layout->removeRow(row: 0);
768
769 QVERIFY(!w1);
770 QCOMPARE(layout->count(), 0);
771 QCOMPARE(layout->rowCount(), 0);
772}
773
774void tst_QFormLayout::removeRow_QWidget()
775{
776 QWidget topLevel;
777 QFormLayout *layout = new QFormLayout(&topLevel);
778
779 QCOMPARE(layout->count(), 0);
780 QCOMPARE(layout->rowCount(), 0);
781
782 QPointer<QWidget> w1 = new QWidget;
783 QPointer<QWidget> w2 = new QWidget;
784
785 layout->addRow(labelText: "test1", field: w1);
786 layout->addRow(widget: w2);
787
788 QCOMPARE(layout->count(), 3);
789 QCOMPARE(layout->rowCount(), 2);
790
791 layout->removeRow(widget: w1);
792
793 QVERIFY(!w1);
794 QCOMPARE(layout->count(), 1);
795 QCOMPARE(layout->rowCount(), 1);
796
797 layout->removeRow(widget: w2);
798
799 QVERIFY(!w2);
800 QCOMPARE(layout->count(), 0);
801 QCOMPARE(layout->rowCount(), 0);
802
803 QWidget *w3 = new QWidget;
804 layout->removeRow(widget: w3);
805 delete w3;
806}
807
808void tst_QFormLayout::removeRow_QLayout()
809{
810 QWidget topLevel;
811 QFormLayout *layout = new QFormLayout(&topLevel);
812
813 QCOMPARE(layout->count(), 0);
814 QCOMPARE(layout->rowCount(), 0);
815
816 QPointer<QHBoxLayout> l1 = new QHBoxLayout;
817 QPointer<QWidget> w1 = new QWidget;
818 l1->addWidget(w1);
819 QPointer<QHBoxLayout> l2 = new QHBoxLayout;
820 QPointer<QWidget> w2 = new QWidget;
821 l2->addWidget(w2);
822
823 layout->addRow(labelText: "test1", field: l1);
824 layout->addRow(layout: l2);
825
826 QCOMPARE(layout->count(), 3);
827 QCOMPARE(layout->rowCount(), 2);
828
829 layout->removeRow(layout: l1);
830
831 QVERIFY(!l1);
832 QVERIFY(!w1);
833 QCOMPARE(layout->count(), 1);
834 QCOMPARE(layout->rowCount(), 1);
835
836 layout->removeRow(layout: l2);
837
838 QVERIFY(!l2);
839 QVERIFY(!w2);
840 QCOMPARE(layout->count(), 0);
841 QCOMPARE(layout->rowCount(), 0);
842
843 QHBoxLayout *l3 = new QHBoxLayout;
844 layout->removeRow(layout: l3);
845 delete l3;
846}
847
848void tst_QFormLayout::takeRow()
849{
850 QWidget topLevel;
851 QFormLayout *layout = new QFormLayout(&topLevel);
852
853 QCOMPARE(layout->count(), 0);
854 QCOMPARE(layout->rowCount(), 0);
855
856 QPointer<QWidget> w1 = new QWidget;
857 QPointer<QWidget> w2 = new QWidget;
858
859 layout->addRow(labelText: "test1", field: w1);
860 layout->addRow(widget: w2);
861
862 QCOMPARE(layout->count(), 3);
863 QCOMPARE(layout->rowCount(), 2);
864
865 QFormLayoutTakeRowResultHolder result = layout->takeRow(row: 1);
866
867 QVERIFY(w2);
868 QVERIFY(result.fieldItem);
869 QVERIFY(!result.labelItem);
870 QCOMPARE(layout->count(), 2);
871 QCOMPARE(layout->rowCount(), 1);
872 QCOMPARE(result.fieldItem->widget(), w2.data());
873
874 result = layout->takeRow(row: 0);
875
876 QVERIFY(w1);
877 QVERIFY(result.fieldItem);
878 QVERIFY(result.labelItem);
879 QCOMPARE(layout->count(), 0);
880 QCOMPARE(layout->rowCount(), 0);
881 QCOMPARE(result.fieldItem->widget(), w1.data());
882
883 result = layout->takeRow(row: 0);
884
885 QVERIFY(!result.fieldItem);
886 QVERIFY(!result.labelItem);
887}
888
889void tst_QFormLayout::takeRow_QWidget()
890{
891 QWidget topLevel;
892 QFormLayout *layout = new QFormLayout(&topLevel);
893
894 QCOMPARE(layout->count(), 0);
895 QCOMPARE(layout->rowCount(), 0);
896
897 QPointer<QWidget> w1 = new QWidget;
898 QPointer<QWidget> w2 = new QWidget;
899
900 layout->addRow(labelText: "test1", field: w1);
901 layout->addRow(widget: w2);
902
903 QCOMPARE(layout->count(), 3);
904 QCOMPARE(layout->rowCount(), 2);
905
906 QFormLayoutTakeRowResultHolder result = layout->takeRow(widget: w1);
907
908 QVERIFY(w1);
909 QVERIFY(result.fieldItem);
910 QVERIFY(result.labelItem);
911 QCOMPARE(layout->count(), 1);
912 QCOMPARE(layout->rowCount(), 1);
913
914 result = layout->takeRow(widget: w2);
915
916 QVERIFY(w2);
917 QVERIFY(result.fieldItem);
918 QVERIFY(!result.labelItem);
919 QCOMPARE(layout->count(), 0);
920 QCOMPARE(layout->rowCount(), 0);
921
922 QWidget *w3 = new QWidget;
923 result = layout->takeRow(widget: w3);
924 delete w3;
925
926 QVERIFY(!result.fieldItem);
927 QVERIFY(!result.labelItem);
928}
929
930void tst_QFormLayout::takeRow_QLayout()
931{
932 QWidget topLevel;
933 QFormLayout *layout = new QFormLayout(&topLevel);
934
935 QCOMPARE(layout->count(), 0);
936 QCOMPARE(layout->rowCount(), 0);
937
938 QPointer<QHBoxLayout> l1 = new QHBoxLayout;
939 QPointer<QWidget> w1 = new QWidget;
940 l1->addWidget(w1);
941 QPointer<QHBoxLayout> l2 = new QHBoxLayout;
942 QPointer<QWidget> w2 = new QWidget;
943 l2->addWidget(w2);
944
945 layout->addRow(labelText: "test1", field: l1);
946 layout->addRow(layout: l2);
947
948 QCOMPARE(layout->count(), 3);
949 QCOMPARE(layout->rowCount(), 2);
950
951 QFormLayoutTakeRowResultHolder result = layout->takeRow(layout: l1);
952
953 QVERIFY(l1);
954 QVERIFY(w1);
955 QVERIFY(result.fieldItem);
956 QVERIFY(result.labelItem);
957 QCOMPARE(layout->count(), 1);
958 QCOMPARE(layout->rowCount(), 1);
959
960 result = layout->takeRow(layout: l2);
961
962 QVERIFY(l2);
963 QVERIFY(w2);
964 QVERIFY(result.fieldItem);
965 QVERIFY(!result.labelItem);
966 QCOMPARE(layout->count(), 0);
967 QCOMPARE(layout->rowCount(), 0);
968
969 QHBoxLayout *l3 = new QHBoxLayout;
970 result = layout->takeRow(layout: l3);
971 delete l3;
972
973 QVERIFY(!result.fieldItem);
974 QVERIFY(!result.labelItem);
975}
976
977void tst_QFormLayout::setWidget()
978{
979 QFormLayout layout;
980
981 QWidget w1;
982 QWidget w2;
983 QWidget w3;
984 QWidget w4;
985
986 QCOMPARE(layout.count(), 0);
987 QCOMPARE(layout.rowCount(), 0);
988
989 layout.setWidget(row: 5, role: QFormLayout::LabelRole, widget: &w1);
990 QCOMPARE(layout.count(), 1);
991 QCOMPARE(layout.rowCount(), 6);
992
993 layout.setWidget(row: 3, role: QFormLayout::FieldRole, widget: &w2);
994 layout.setWidget(row: 3, role: QFormLayout::LabelRole, widget: &w3);
995 QCOMPARE(layout.count(), 3);
996 QCOMPARE(layout.rowCount(), 6);
997
998 // should be ignored and generate warnings
999 layout.setWidget(row: 3, role: QFormLayout::FieldRole, widget: &w4);
1000 layout.setWidget(row: -1, role: QFormLayout::FieldRole, widget: &w4);
1001
1002 {
1003 int row = -1;
1004 QFormLayout::ItemRole role = invalidRole;
1005 layout.getWidgetPosition(widget: &w1, rowPtr: &row, rolePtr: &role);
1006 QCOMPARE(row, 5);
1007 QCOMPARE(int(role), int(QFormLayout::LabelRole));
1008 }
1009
1010 {
1011 int row = -1;
1012 QFormLayout::ItemRole role = invalidRole;
1013 layout.getWidgetPosition(widget: &w2, rowPtr: &row, rolePtr: &role);
1014 QCOMPARE(row, 3);
1015 QCOMPARE(int(role), int(QFormLayout::FieldRole));
1016 }
1017
1018 {
1019 int row = -1;
1020 QFormLayout::ItemRole role = invalidRole;
1021 layout.getWidgetPosition(widget: &w3, rowPtr: &row, rolePtr: &role);
1022 QCOMPARE(row, 3);
1023 QCOMPARE(int(role), int(QFormLayout::LabelRole));
1024 }
1025
1026 {
1027 int row = -1;
1028 QFormLayout::ItemRole role = invalidRole;
1029 layout.getWidgetPosition(widget: &w4, rowPtr: &row, rolePtr: &role);
1030 // not found
1031 QCOMPARE(row, -1);
1032 QCOMPARE(int(role), int(invalidRole));
1033 }
1034
1035 {
1036 int row = -1;
1037 QFormLayout::ItemRole role = invalidRole;
1038 layout.getWidgetPosition(widget: 0, rowPtr: &row, rolePtr: &role);
1039 // not found
1040 QCOMPARE(row, -1);
1041 QCOMPARE(int(role), int(invalidRole));
1042 }
1043}
1044
1045void tst_QFormLayout::setLayout()
1046{
1047 QFormLayout layout;
1048
1049 QHBoxLayout l1;
1050 QHBoxLayout l2;
1051 QHBoxLayout l3;
1052 QHBoxLayout l4;
1053
1054 QCOMPARE(layout.count(), 0);
1055 QCOMPARE(layout.rowCount(), 0);
1056
1057 layout.setLayout(row: 5, role: QFormLayout::LabelRole, layout: &l1);
1058 QCOMPARE(layout.count(), 1);
1059 QCOMPARE(layout.rowCount(), 6);
1060
1061 layout.setLayout(row: 3, role: QFormLayout::FieldRole, layout: &l2);
1062 layout.setLayout(row: 3, role: QFormLayout::LabelRole, layout: &l3);
1063 QCOMPARE(layout.count(), 3);
1064 QCOMPARE(layout.rowCount(), 6);
1065
1066 // should be ignored and generate warnings
1067 layout.setLayout(row: 3, role: QFormLayout::FieldRole, layout: &l4);
1068 layout.setLayout(row: -1, role: QFormLayout::FieldRole, layout: &l4);
1069 QCOMPARE(layout.count(), 3);
1070 QCOMPARE(layout.rowCount(), 6);
1071
1072 {
1073 int row = -1;
1074 QFormLayout::ItemRole role = invalidRole;
1075 layout.getLayoutPosition(layout: &l1, rowPtr: &row, rolePtr: &role);
1076 QCOMPARE(row, 5);
1077 QCOMPARE(int(role), int(QFormLayout::LabelRole));
1078 }
1079
1080 {
1081 int row = -1;
1082 QFormLayout::ItemRole role = invalidRole;
1083 layout.getLayoutPosition(layout: &l2, rowPtr: &row, rolePtr: &role);
1084 QCOMPARE(row, 3);
1085 QCOMPARE(int(role), int(QFormLayout::FieldRole));
1086 }
1087
1088 {
1089 int row = -1;
1090 QFormLayout::ItemRole role = invalidRole;
1091 layout.getLayoutPosition(layout: &l3, rowPtr: &row, rolePtr: &role);
1092 QCOMPARE(row, 3);
1093 QCOMPARE(int(role), int(QFormLayout::LabelRole));
1094 }
1095
1096 {
1097 int row = -1;
1098 QFormLayout::ItemRole role = invalidRole;
1099 layout.getLayoutPosition(layout: &l4, rowPtr: &row, rolePtr: &role);
1100 QCOMPARE(row, -1);
1101 QCOMPARE(int(role), int(invalidRole));
1102 }
1103
1104 {
1105 int row = -1;
1106 QFormLayout::ItemRole role = invalidRole;
1107 layout.getLayoutPosition(layout: 0, rowPtr: &row, rolePtr: &role);
1108 QCOMPARE(row, -1);
1109 QCOMPARE(int(role), int(invalidRole));
1110 }
1111}
1112
1113void tst_QFormLayout::itemAt()
1114{
1115 QWidget topLevel;
1116 QFormLayout *layout = new QFormLayout(&topLevel);
1117
1118 QWidget *w1 = new QWidget(&topLevel);
1119 QWidget *w2 = new QWidget(&topLevel);
1120 QWidget *w3 = new QWidget(&topLevel);
1121 QWidget *w4 = new QWidget(&topLevel);
1122 QWidget *w5 = new QWidget(&topLevel);
1123 QHBoxLayout *l6 = new QHBoxLayout;
1124
1125 layout->setWidget(row: 5, role: QFormLayout::LabelRole, widget: w1);
1126 layout->setWidget(row: 3, role: QFormLayout::FieldRole, widget: w2);
1127 layout->setWidget(row: 3, role: QFormLayout::LabelRole, widget: w3);
1128 layout->addRow(label: w4, field: w5);
1129 layout->addRow(labelText: "Foo:", field: l6);
1130
1131 QCOMPARE(layout->count(), 7);
1132
1133 QBitArray scoreBoard(7);
1134 for (int i = 0; i < 7; ++i) {
1135 QLayoutItem *item = layout->itemAt(index: i);
1136 QVERIFY(item != 0);
1137
1138 if (item->widget() == w1) {
1139 scoreBoard[0] = true;
1140 } else if (item->widget() == w2) {
1141 scoreBoard[1] = true;
1142 } else if (item->widget() == w3) {
1143 scoreBoard[2] = true;
1144 } else if (item->widget() == w4) {
1145 scoreBoard[3] = true;
1146 } else if (item->widget() == w5) {
1147 scoreBoard[4] = true;
1148 } else if (item->layout() == l6) {
1149 scoreBoard[5] = true;
1150 } else if (qobject_cast<QLabel *>(object: item->widget())) {
1151 scoreBoard[6] = true;
1152 }
1153 }
1154 QCOMPARE(scoreBoard.count(false), 0);
1155}
1156
1157void tst_QFormLayout::takeAt()
1158{
1159 QWidget topLevel;
1160 QFormLayout *layout = new QFormLayout(&topLevel);
1161
1162 QWidget *w1 = new QWidget(&topLevel);
1163 QWidget *w2 = new QWidget(&topLevel);
1164 QWidget *w3 = new QWidget(&topLevel);
1165 QWidget *w4 = new QWidget(&topLevel);
1166 QWidget *w5 = new QWidget(&topLevel);
1167 QHBoxLayout *l6 = new QHBoxLayout;
1168
1169 layout->setWidget(row: 5, role: QFormLayout::LabelRole, widget: w1);
1170 layout->setWidget(row: 3, role: QFormLayout::FieldRole, widget: w2);
1171 layout->setWidget(row: 3, role: QFormLayout::LabelRole, widget: w3);
1172 layout->addRow(label: w4, field: w5);
1173 layout->addRow(labelText: "Foo:", field: l6);
1174
1175 QCOMPARE(layout->count(), 7);
1176
1177 for (int i = 6; i >= 0; --i) {
1178 delete layout->takeAt(index: 0);
1179 QCOMPARE(layout->count(), i);
1180 }
1181}
1182
1183void tst_QFormLayout::layoutAlone()
1184{
1185 QWidget w;
1186 QFormLayout layout;
1187 layout.setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
1188 w.setLayout(&layout);
1189 QLabel label("Here is a strange test case");
1190 layout.setWidget(row: 0, role: QFormLayout::LabelRole, widget: &label);
1191 QHBoxLayout hlay;
1192 layout.setLayout(row: 1, role: QFormLayout::LabelRole, layout: &hlay);
1193 QCOMPARE(layout.count(), 2);
1194 w.setWindowTitle(QTest::currentTestFunction());
1195 w.show();
1196 layout.activate();
1197}
1198
1199void tst_QFormLayout::taskQTBUG_27420_takeAtShouldUnparentLayout()
1200{
1201 QSharedPointer<QFormLayout> outer(new QFormLayout);
1202 QAutoPointer<QFormLayout> holder{new QFormLayout};
1203 auto inner = holder.get();
1204
1205 outer->addRow(layout: inner);
1206 QCOMPARE(outer->count(), 1);
1207 QCOMPARE(inner->parent(), outer.data());
1208
1209 QLayoutItem *item = outer->takeAt(index: 0);
1210 QCOMPARE(item->layout(), inner);
1211 QVERIFY(!item->layout()->parent());
1212
1213 outer.reset();
1214
1215 QVERIFY(holder); // a taken item/layout should not be deleted when the old parent is deleted
1216}
1217
1218void tst_QFormLayout::taskQTBUG_40609_addingWidgetToItsOwnLayout(){
1219 QWidget widget;
1220 widget.setObjectName("6435cbada60548b4522cbb6");
1221 QFormLayout layout(&widget);
1222 layout.setObjectName("c03c0e22c0b6d019a93a248");
1223
1224 QTest::ignoreMessage(type: QtWarningMsg, message: "QLayout: Cannot add parent widget QWidget/6435cbada60548b4522cbb6 to its child layout QFormLayout/c03c0e22c0b6d019a93a248");
1225 layout.addRow(labelText: QLatin1String("48c81f39b7320082f8"), field: &widget);
1226 QCOMPARE(layout.count(), 0);
1227}
1228
1229void tst_QFormLayout::taskQTBUG_40609_addingLayoutToItself(){
1230 QWidget widget;
1231 widget.setObjectName("2bc425637d084c07ce65956");
1232 QFormLayout layout(&widget);
1233 layout.setObjectName("60e31de0c8800eaba713a4f2");
1234
1235 QTest::ignoreMessage(type: QtWarningMsg, message: "QLayout: Cannot add layout QFormLayout/60e31de0c8800eaba713a4f2 to itself");
1236 layout.addRow(labelText: QLatin1String("9a2cd4f40c06b489f889"), field: &layout);
1237 QCOMPARE(layout.count(), 0);
1238}
1239
1240void tst_QFormLayout::replaceWidget()
1241{
1242 QWidget w;
1243 QFormLayout *layout = new QFormLayout();
1244 w.setLayout(layout);
1245 QLineEdit *edit1 = new QLineEdit();
1246 QLineEdit *edit2 = new QLineEdit();
1247 QLineEdit *edit3 = new QLineEdit();
1248 QLabel *label1 = new QLabel();
1249 QLabel *label2 = new QLabel();
1250
1251 layout->addRow(labelText: "Label", field: edit1);
1252 layout->addRow(label: label1, field: edit2);
1253
1254 // Verify controls not in layout
1255 QCOMPARE(layout->indexOf(edit3), -1);
1256 QCOMPARE(layout->indexOf(label2), -1);
1257
1258 // Verify controls in layout
1259 int editIndex = layout->indexOf(edit1);
1260 int labelIndex = layout->indexOf(label1);
1261 QVERIFY(editIndex > 0);
1262 QVERIFY(labelIndex > 0);
1263 int rownum;
1264 QFormLayout::ItemRole role;
1265
1266 // replace editor
1267 delete layout->replaceWidget(from: edit1, to: edit3);
1268 edit1->hide(); // Not strictly needed for the test, but for normal usage it is.
1269 QCOMPARE(layout->indexOf(edit1), -1);
1270 QCOMPARE(layout->indexOf(edit3), editIndex);
1271 QCOMPARE(layout->indexOf(label1), labelIndex);
1272 rownum = -1;
1273 role = QFormLayout::SpanningRole;
1274 layout->getWidgetPosition(widget: edit3, rowPtr: &rownum, rolePtr: &role);
1275 QCOMPARE(rownum, 0);
1276 QCOMPARE(role, QFormLayout::FieldRole);
1277
1278 delete layout->replaceWidget(from: label1, to: label2);
1279 label1->hide();
1280 QCOMPARE(layout->indexOf(label1), -1);
1281 QCOMPARE(layout->indexOf(label2), labelIndex);
1282 layout->getWidgetPosition(widget: label2, rowPtr: &rownum, rolePtr: &role);
1283 QCOMPARE(rownum, 1);
1284 QCOMPARE(role, QFormLayout::LabelRole);
1285
1286}
1287
1288QTEST_MAIN(tst_QFormLayout)
1289
1290#include "tst_qformlayout.moc"
1291

source code of qtbase/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp