void tst_QFormLayout::getPosition() { QWidget *w = new QWidget; QFormLayout *fl = new QFormLayout(w); QList<QLabel*> labels; QList<QLineEdit*> fields; for (int i = 0; i < 5; ++i) { labels.append(new QLabel(QString("Label %1").arg(i+1))); fields.append(new QLineEdit); fl->addRow(labels[i], fields[i]); } //a field { int row; QFormLayout::ItemRole role; fl->getWidgetPosition(fields[3], &row, &role); QCOMPARE(row, 3); QCOMPARE(role, QFormLayout::FieldRole); int old_row = row; QFormLayout::ItemRole old_role = role; fl->getItemPosition(3*2 + 1, &row, &role); QCOMPARE(row, old_row); QCOMPARE(role, old_role); } //a label { int row; QFormLayout::ItemRole role; fl->getWidgetPosition(labels[2], &row, &role); QCOMPARE(row, 2); QCOMPARE(role, QFormLayout::LabelRole); int old_row = row; QFormLayout::ItemRole old_role = role; fl->getItemPosition(2*2, &row, &role); QCOMPARE(row, old_row); QCOMPARE(role, old_role); } //a layout that's been inserted { QVBoxLayout *vbl = new QVBoxLayout; fl->insertRow(2, "Label 1.5", vbl); int row; QFormLayout::ItemRole role; fl->getLayoutPosition(vbl, &row, &role); QCOMPARE(row, 2); QCOMPARE(role, QFormLayout::FieldRole); int old_row = row; QFormLayout::ItemRole old_role = role; fl->getItemPosition(labels.count()*2 + 1, &row, &role); QCOMPARE(row, old_row); QCOMPARE(role, old_role); } delete w; }
void tst_QFormLayout::replaceWidget() { QWidget w; QFormLayout *layout = new QFormLayout(); w.setLayout(layout); QLineEdit *edit1 = new QLineEdit(); QLineEdit *edit2 = new QLineEdit(); QLineEdit *edit3 = new QLineEdit(); QLabel *label1 = new QLabel(); QLabel *label2 = new QLabel(); layout->addRow("Label", edit1); layout->addRow(label1, edit2); // Verify controls not in layout QCOMPARE(layout->indexOf(edit3), -1); QCOMPARE(layout->indexOf(label2), -1); // Verify controls in layout int editIndex = layout->indexOf(edit1); int labelIndex = layout->indexOf(label1); QVERIFY(editIndex > 0); QVERIFY(labelIndex > 0); int rownum; QFormLayout::ItemRole role; // replace editor layout->replaceWidget(edit1, edit3); edit1->hide(); // Not strictly needed for the test, but for normal usage it is. QCOMPARE(layout->indexOf(edit1), -1); QCOMPARE(layout->indexOf(edit3), editIndex); QCOMPARE(layout->indexOf(label1), labelIndex); rownum = -1; role = QFormLayout::SpanningRole; layout->getWidgetPosition(edit3, &rownum, &role); QCOMPARE(rownum, 0); QCOMPARE(role, QFormLayout::FieldRole); layout->replaceWidget(label1, label2); label1->hide(); QCOMPARE(layout->indexOf(label1), -1); QCOMPARE(layout->indexOf(label2), labelIndex); layout->getWidgetPosition(label2, &rownum, &role); QCOMPARE(rownum, 1); QCOMPARE(role, QFormLayout::LabelRole); }
void tst_QFormLayout::insertRow_QWidget_QLayout() { QFormLayout layout; QLabel lbl1, lbl2, lbl3, lbl4; QHBoxLayout fld1, fld2, fld3, fld4; layout.insertRow(0, &lbl1, &fld1); QCOMPARE(layout.rowCount(), 1); { int row = -1; QFormLayout::ItemRole role = QFormLayout::ItemRole(-123); layout.getWidgetPosition(&lbl1, &row, &role); QCOMPARE(row, 0); QCOMPARE(int(role), int(QFormLayout::LabelRole)); } { int row = -1; QFormLayout::ItemRole role = QFormLayout::ItemRole(-123); layout.getLayoutPosition(&fld1, &row, &role); QCOMPARE(row, 0); QCOMPARE(int(role), int(QFormLayout::FieldRole)); } // check that negative values append layout.insertRow(-2, &lbl2, &fld2); QCOMPARE(layout.rowCount(), 2); QVERIFY(layout.itemAt(0, QFormLayout::LabelRole)->widget() == &lbl1); QVERIFY(layout.itemAt(1, QFormLayout::LabelRole)->widget() == &lbl2); // check that too large values append layout.insertRow(100, &lbl3, &fld3); QCOMPARE(layout.rowCount(), 3); QVERIFY(layout.itemAt(0, QFormLayout::LabelRole)->widget() == &lbl1); QVERIFY(layout.itemAt(1, QFormLayout::LabelRole)->widget() == &lbl2); QVERIFY(layout.itemAt(2, QFormLayout::LabelRole)->widget() == &lbl3); QVERIFY(layout.itemAt(0, QFormLayout::FieldRole)->layout() == &fld1); QVERIFY(layout.itemAt(1, QFormLayout::FieldRole)->layout() == &fld2); QVERIFY(layout.itemAt(2, QFormLayout::FieldRole)->layout() == &fld3); }
void tst_QFormLayout::setWidget() { QFormLayout layout; QWidget w1; QWidget w2; QWidget w3; QWidget w4; QCOMPARE(layout.count(), 0); QCOMPARE(layout.rowCount(), 0); layout.setWidget(5, QFormLayout::LabelRole, &w1); QCOMPARE(layout.count(), 1); QCOMPARE(layout.rowCount(), 6); layout.setWidget(3, QFormLayout::FieldRole, &w2); layout.setWidget(3, QFormLayout::LabelRole, &w3); QCOMPARE(layout.count(), 3); QCOMPARE(layout.rowCount(), 6); // should be ignored and generate warnings layout.setWidget(3, QFormLayout::FieldRole, &w4); layout.setWidget(-1, QFormLayout::FieldRole, &w4); { int row = -1; QFormLayout::ItemRole role = QFormLayout::ItemRole(-123); layout.getWidgetPosition(&w1, &row, &role); QCOMPARE(row, 5); QCOMPARE(int(role), int(QFormLayout::LabelRole)); } { int row = -1; QFormLayout::ItemRole role = QFormLayout::ItemRole(-123); layout.getWidgetPosition(&w2, &row, &role); QCOMPARE(row, 3); QCOMPARE(int(role), int(QFormLayout::FieldRole)); } { int row = -1; QFormLayout::ItemRole role = QFormLayout::ItemRole(-123); layout.getWidgetPosition(&w3, &row, &role); QCOMPARE(row, 3); QCOMPARE(int(role), int(QFormLayout::LabelRole)); } { int row = -1; QFormLayout::ItemRole role = QFormLayout::ItemRole(-123); layout.getWidgetPosition(&w4, &row, &role); QCOMPARE(row, -1); QCOMPARE(int(role), -123); } { int row = -1; QFormLayout::ItemRole role = QFormLayout::ItemRole(-123); layout.getWidgetPosition(0, &row, &role); QCOMPARE(row, -1); QCOMPARE(int(role), -123); } }
QFormLayout::ItemRole getWidgetRole(const QFormLayout& layout, QWidget* widget) { int row; QFormLayout::ItemRole role; layout.getWidgetPosition(widget, &row, &role); return role; }