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