diff options
Diffstat (limited to 'tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp')
-rw-r--r-- | tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp | 106 |
1 files changed, 88 insertions, 18 deletions
diff --git a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp index 68a7268473..aa17026daf 100644 --- a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp +++ b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp @@ -205,6 +205,8 @@ private slots: void dateTextForRole_data(); void dateTextForRole(); + void reuseEditor(); + private: #ifdef QT_BUILD_INTERNAL struct RoleDelegate : public QItemDelegate @@ -325,7 +327,7 @@ void tst_QItemDelegate::editorKeyPress() view.edit(index); QList<QLineEdit*> lineEditors = view.viewport()->findChildren<QLineEdit *>(); - QCOMPARE(lineEditors.count(), 1); + QCOMPARE(lineEditors.size(), 1); QLineEdit *editor = lineEditors.at(0); QCOMPARE(editor->selectedText(), initial); @@ -353,7 +355,7 @@ void tst_QItemDelegate::doubleEditorNegativeInput() view.edit(index); QList<QDoubleSpinBox*> editors = view.viewport()->findChildren<QDoubleSpinBox *>(); - QCOMPARE(editors.count(), 1); + QCOMPARE(editors.size(), 1); QDoubleSpinBox *editor = editors.at(0); QCOMPARE(editor->value(), double(10)); @@ -670,33 +672,33 @@ void tst_QItemDelegate::testEventFilter() //For each test we send a key event and check if signals were emitted. event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier); QVERIFY(delegate.eventFilter(&widget, event)); - QCOMPARE(closeEditorSpy.count(), 1); - QCOMPARE(commitDataSpy.count(), 1); + QCOMPARE(closeEditorSpy.size(), 1); + QCOMPARE(commitDataSpy.size(), 1); delete event; event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Backtab, Qt::NoModifier); QVERIFY(delegate.eventFilter(&widget, event)); - QCOMPARE(closeEditorSpy.count(), 2); - QCOMPARE(commitDataSpy.count(), 2); + QCOMPARE(closeEditorSpy.size(), 2); + QCOMPARE(commitDataSpy.size(), 2); delete event; event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier); QVERIFY(delegate.eventFilter(&widget, event)); - QCOMPARE(closeEditorSpy.count(), 3); - QCOMPARE(commitDataSpy.count(), 2); + QCOMPARE(closeEditorSpy.size(), 3); + QCOMPARE(commitDataSpy.size(), 2); delete event; event = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier); QVERIFY(!delegate.eventFilter(&widget, event)); - QCOMPARE(closeEditorSpy.count(), 3); - QCOMPARE(commitDataSpy.count(), 2); + QCOMPARE(closeEditorSpy.size(), 3); + QCOMPARE(commitDataSpy.size(), 2); delete event; //Subtest focusEvent event = new QFocusEvent(QEvent::FocusOut); QVERIFY(!delegate.eventFilter(&widget, event)); - QCOMPARE(closeEditorSpy.count(), 4); - QCOMPARE(commitDataSpy.count(), 3); + QCOMPARE(closeEditorSpy.size(), 4); + QCOMPARE(commitDataSpy.size(), 3); delete event; } @@ -1312,7 +1314,7 @@ void tst_QItemDelegate::enterKey() view.edit(index); QList<QWidget*> lineEditors = view.viewport()->findChildren<QWidget *>(QString::fromLatin1("TheEditor")); - QCOMPARE(lineEditors.count(), 1); + QCOMPARE(lineEditors.size(), 1); QPointer<QWidget> editor = lineEditors.at(0); QCOMPARE(editor->hasFocus(), true); @@ -1346,7 +1348,7 @@ void tst_QItemDelegate::task257859_finalizeEdit() view.edit(index); QList<QLineEdit *> lineEditors = view.viewport()->findChildren<QLineEdit *>(); - QCOMPARE(lineEditors.count(), 1); + QCOMPARE(lineEditors.size(), 1); QPointer<QWidget> editor = lineEditors.at(0); QCOMPARE(editor->hasFocus(), true); @@ -1477,7 +1479,7 @@ void tst_QItemDelegate::testLineEditValidation() const auto findEditors = [&]() { return view.findChildren<QLineEdit *>(QStringLiteral("TheEditor")); }; - QCOMPARE(findEditors().count(), 1); + QCOMPARE(findEditors().size(), 1); editor = findEditors().at(0); editor->clear(); @@ -1497,7 +1499,7 @@ void tst_QItemDelegate::testLineEditValidation() view.setCurrentIndex(index); view.edit(index); - QTRY_COMPARE(findEditors().count(), 1); + QTRY_COMPARE(findEditors().size(), 1); editor = findEditors().at(0); editor->clear(); @@ -1519,13 +1521,13 @@ void tst_QItemDelegate::testLineEditValidation() // reset the view to forcibly close the editor view.reset(); - QTRY_COMPARE(findEditors().count(), 0); + QTRY_COMPARE(findEditors().size(), 0); // set a valid text again view.setCurrentIndex(index); view.edit(index); - QTRY_COMPARE(findEditors().count(), 1); + QTRY_COMPARE(findEditors().size(), 1); editor = findEditors().at(0); editor->clear(); @@ -1617,6 +1619,74 @@ void tst_QItemDelegate::dateTextForRole() #endif } +void tst_QItemDelegate::reuseEditor() +{ + class ReusingDelegate: public QItemDelegate { + public: + using QItemDelegate::QItemDelegate; + ~ReusingDelegate() + { + cached->deleteLater(); + } + + QWidget* createEditor(QWidget* parent, + const QStyleOptionViewItem&, + const QModelIndex&) const override + { + auto *cb = new QComboBox(parent); + cb->addItem("One"); + cb->addItem("Two"); + cb->setEditable(true); + return cb; + } + + void setEditorData(QWidget* editor, const QModelIndex& index) + const override + { + auto *cb = qobject_cast<QComboBox*>(editor); + cb->setCurrentText(index.data(Qt::DisplayRole).toString()); + } + + void setModelData(QWidget* editor, + QAbstractItemModel* model, + const QModelIndex& index) const override + { + auto *cb = qobject_cast<QComboBox*>(editor); + model->setData(index, cb->currentText(), Qt::DisplayRole); + } + + void destroyEditor(QWidget* editor, const QModelIndex&) const override + { + auto *cb = qobject_cast<QComboBox*>(editor); + cb->setParent(nullptr); // How to completely detach the editor from treeview ? + cb->hide(); + cb->setEnabled(false); + cached = cb; + } + + private: + mutable QComboBox* cached = nullptr; + }; + + QStandardItemModel model; + model.appendRow(new QStandardItem("One")); + model.appendRow(new QStandardItem("Two")); + + ReusingDelegate delegate; + + QTreeView tree; + tree.setModel(&model); + tree.setItemDelegate(&delegate); + + tree.show(); + QVERIFY(QTest::qWaitForWindowActive(&tree)); + + tree.edit(model.index(0, 0)); + QTRY_VERIFY(qobject_cast<QComboBox *>(tree.focusWidget())); + + tree.close(); +} + // ### _not_ covered: // editing with a custom editor factory |