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 <QDoubleSpinBox>
31#include <QItemEditorFactory>
32#include <QTest>
33
34class tst_QItemEditorFactory: public QObject
35{
36 Q_OBJECT
37private slots:
38 void createEditor();
39 void createCustomEditor();
40 void uintValues();
41};
42
43void tst_QItemEditorFactory::createEditor()
44{
45 const QItemEditorFactory *factory = QItemEditorFactory::defaultFactory();
46
47 QWidget parent;
48
49 QWidget *w = factory->createEditor(userType: QVariant::String, parent: &parent);
50 QCOMPARE(w->metaObject()->className(), "QExpandingLineEdit");
51}
52
53//we make it inherit from QObject so that we can use QPointer
54class MyEditor : public QObject, public QStandardItemEditorCreator<QDoubleSpinBox>
55{
56};
57
58void tst_QItemEditorFactory::createCustomEditor()
59{
60 QPointer<MyEditor> creator = new MyEditor;
61 QPointer<MyEditor> creator2 = new MyEditor;
62
63 {
64 QItemEditorFactory editorFactory;
65
66 editorFactory.registerEditor(userType: QVariant::Rect, creator);
67 editorFactory.registerEditor(userType: QVariant::RectF, creator);
68
69 //creator should not be deleted as a result of calling the next line
70 editorFactory.registerEditor(userType: QVariant::Rect, creator: creator2);
71 QVERIFY(creator);
72
73 //this should erase creator2
74 editorFactory.registerEditor(userType: QVariant::Rect, creator);
75 QVERIFY(creator2.isNull());
76
77
78 QWidget parent;
79
80 QWidget *w = editorFactory.createEditor(userType: QVariant::Rect, parent: &parent);
81 QCOMPARE(w->metaObject()->className(), "QDoubleSpinBox");
82 QCOMPARE(w->metaObject()->userProperty().type(), QVariant::Double);
83 }
84
85 //editorFactory has been deleted, so should be creator
86 //because editorFActory has the ownership
87 QVERIFY(creator.isNull());
88 QVERIFY(creator2.isNull());
89
90 delete creator;
91}
92
93void tst_QItemEditorFactory::uintValues()
94{
95 QItemEditorFactory editorFactory;
96
97 QWidget parent;
98
99 {
100 QWidget *editor = editorFactory.createEditor(userType: QMetaType::UInt, parent: &parent);
101 QCOMPARE(editor->metaObject()->className(), "QUIntSpinBox");
102 QCOMPARE(editor->metaObject()->userProperty().type(), QVariant::UInt);
103 }
104 {
105 QWidget *editor = editorFactory.createEditor(userType: QMetaType::Int, parent: &parent);
106 QCOMPARE(editor->metaObject()->className(), "QSpinBox");
107 QCOMPARE(editor->metaObject()->userProperty().type(), QVariant::Int);
108 }
109}
110
111QTEST_MAIN(tst_QItemEditorFactory)
112#include "tst_qitemeditorfactory.moc"
113
114

source code of qtbase/tests/auto/widgets/itemviews/qitemeditorfactory/tst_qitemeditorfactory.cpp