summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp')
-rw-r--r--tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp167
1 files changed, 107 insertions, 60 deletions
diff --git a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
index ad133b0422..e1488e4484 100644
--- a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
+++ b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
@@ -1,35 +1,11 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "../../../shared/highdpi.h"
#include <QTest>
#include <QTimeZone>
+#include <QDateTime>
#include <QTimer>
#include <QTestEventLoop>
#include <QSignalSpy>
@@ -59,6 +35,7 @@
#include <qscreen.h>
#include <QtWidgets/private/qabstractitemdelegate_p.h>
+#include <QtWidgets/private/qapplication_p.h>
Q_DECLARE_METATYPE(QAbstractItemDelegate::EndEditHint)
@@ -230,6 +207,8 @@ private slots:
void dateTextForRole_data();
void dateTextForRole();
+ void reuseEditor();
+
private:
#ifdef QT_BUILD_INTERNAL
struct RoleDelegate : public QItemDelegate
@@ -350,16 +329,15 @@ 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);
QTest::keyClicks(editor, expected);
QTest::keyClick(editor, Qt::Key_Enter);
- QApplication::processEvents();
- QCOMPARE(index.data().toString(), expected);
+ QTRY_COMPARE(index.data().toString(), expected);
}
void tst_QItemDelegate::doubleEditorNegativeInput()
@@ -379,7 +357,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));
@@ -696,33 +674,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;
}
@@ -769,7 +747,7 @@ void tst_QItemDelegate::dateTimeEditor()
widget.setItem(0, 2, item3);
widget.show();
QVERIFY(QTest::qWaitForWindowExposed(&widget));
- QApplication::setActiveWindow(&widget);
+ QApplicationPrivate::setActiveWindow(&widget);
widget.editItem(item1);
@@ -785,7 +763,7 @@ void tst_QItemDelegate::dateTimeEditor()
timeEditor->setTime(time.addSecs(60));
widget.clearFocus();
- qApp->setActiveWindow(&widget);
+ QApplicationPrivate::setActiveWindow(&widget);
widget.setFocus();
widget.editItem(item2);
@@ -1049,7 +1027,7 @@ void tst_QItemDelegate::decoration()
TestItemDelegate delegate;
table.setItemDelegate(&delegate);
table.show();
- QApplication::setActiveWindow(&table);
+ QApplicationPrivate::setActiveWindow(&table);
QVERIFY(QTest::qWaitForWindowActive(&table));
QVariant value;
@@ -1245,7 +1223,7 @@ void tst_QItemDelegate::editorEvent()
QPoint pos = inCheck ? qApp->style()->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &option, 0).center() + QPoint(checkMargin, 0) : QPoint(200,200);
QEvent *event = new QMouseEvent((QEvent::Type)type,
- pos,
+ pos, pos,
(Qt::MouseButton)button,
(Qt::MouseButton)button,
Qt::NoModifier);
@@ -1304,7 +1282,7 @@ void tst_QItemDelegate::enterKey()
QListView view;
view.setModel(&model);
view.show();
- QApplication::setActiveWindow(&view);
+ QApplicationPrivate::setActiveWindow(&view);
view.setFocus();
QVERIFY(QTest::qWaitForWindowActive(&view));
@@ -1338,7 +1316,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);
@@ -1364,7 +1342,7 @@ void tst_QItemDelegate::task257859_finalizeEdit()
QListView view;
view.setModel(&model);
view.show();
- QApplication::setActiveWindow(&view);
+ QApplicationPrivate::setActiveWindow(&view);
view.setFocus();
QVERIFY(QTest::qWaitForWindowActive(&view));
@@ -1372,7 +1350,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);
@@ -1426,7 +1404,7 @@ void tst_QItemDelegate::comboBox()
widget.setItem(0, 0, item1);
widget.show();
QVERIFY(QTest::qWaitForWindowExposed(&widget));
- QApplication::setActiveWindow(&widget);
+ QApplicationPrivate::setActiveWindow(&widget);
widget.editItem(item1);
@@ -1491,7 +1469,7 @@ void tst_QItemDelegate::testLineEditValidation()
view.setItemDelegate(&delegate);
view.show();
view.setFocus();
- QApplication::setActiveWindow(&view);
+ QApplicationPrivate::setActiveWindow(&view);
QVERIFY(QTest::qWaitForWindowActive(&view));
QPointer<QLineEdit> editor;
@@ -1503,7 +1481,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();
@@ -1523,7 +1501,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();
@@ -1545,13 +1523,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();
@@ -1615,12 +1593,12 @@ void tst_QItemDelegate::dateTextForRole_data()
QDate date(2013, 12, 11);
QTime time(10, 9, 8, 765);
// Ensure we exercise every time-spec variant:
- QTest::newRow("local") << QDateTime(date, time, Qt::LocalTime);
- QTest::newRow("UTC") << QDateTime(date, time, Qt::UTC);
-#if QT_CONFIG(timezone)
+ QTest::newRow("local") << QDateTime(date, time);
+ QTest::newRow("UTC") << QDateTime(date, time, QTimeZone::UTC);
+# if QT_CONFIG(timezone)
QTest::newRow("zone") << QDateTime(date, time, QTimeZone("Europe/Dublin"));
-#endif
- QTest::newRow("offset") << QDateTime(date, time, Qt::OffsetFromUTC, 36000);
+# endif
+ QTest::newRow("offset") << QDateTime(date, time, QTimeZone::fromSecondsAheadOfUtc(36000));
#endif
}
@@ -1643,6 +1621,75 @@ void tst_QItemDelegate::dateTextForRole()
#endif
}
+void tst_QItemDelegate::reuseEditor()
+{
+ class ReusingDelegate: public QItemDelegate {
+ public:
+ using QItemDelegate::QItemDelegate;
+ ~ReusingDelegate()
+ {
+ if (cached)
+ 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