| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
|
| 5 |
#include <QDoubleSpinBox> |
| 6 |
#include <QItemEditorFactory> |
| 7 |
#include <QTest> |
| 8 |
|
| 9 |
class tst_QItemEditorFactory: public QObject |
| 10 |
{ |
| 11 |
Q_OBJECT |
| 12 |
private slots: |
| 13 |
void createEditor(); |
| 14 |
void createCustomEditor(); |
| 15 |
void uintValues(); |
| 16 |
}; |
| 17 |
|
| 18 |
void tst_QItemEditorFactory::createEditor() |
| 19 |
{ |
| 20 |
const QItemEditorFactory *factory = QItemEditorFactory::defaultFactory(); |
| 21 |
|
| 22 |
QWidget parent; |
| 23 |
|
| 24 |
QWidget *w = factory->createEditor(QMetaType::QString, &parent); |
| 25 |
QCOMPARE(w->metaObject()->className(), "QExpandingLineEdit"); |
| 26 |
} |
| 27 |
|
| 28 |
//we make it inherit from QObject so that we can use QPointer |
| 29 |
class MyEditor : public QObject, public QStandardItemEditorCreator<QDoubleSpinBox> |
| 30 |
{ |
| 31 |
}; |
| 32 |
|
| 33 |
void tst_QItemEditorFactory::createCustomEditor() |
| 34 |
{ |
| 35 |
QPointer<MyEditor> creator = new MyEditor; |
| 36 |
QPointer<MyEditor> creator2 = new MyEditor; |
| 37 |
|
| 38 |
{ |
| 39 |
QItemEditorFactory editorFactory; |
| 40 |
|
| 41 |
editorFactory.registerEditor(QMetaType::QRect, creator); |
| 42 |
editorFactory.registerEditor(QMetaType::QRectF, creator); |
| 43 |
|
| 44 |
//creator should not be deleted as a result of calling the next line |
| 45 |
editorFactory.registerEditor(QMetaType::QRect, creator2); |
| 46 |
QVERIFY(creator); |
| 47 |
|
| 48 |
//this should erase creator2 |
| 49 |
editorFactory.registerEditor(QMetaType::QRect, creator); |
| 50 |
QVERIFY(creator2.isNull()); |
| 51 |
|
| 52 |
|
| 53 |
QWidget parent; |
| 54 |
|
| 55 |
QWidget *w = editorFactory.createEditor(QMetaType::QRect, &parent); |
| 56 |
QCOMPARE(w->metaObject()->className(), "QDoubleSpinBox"); |
| 57 |
QCOMPARE(w->metaObject()->userProperty().userType(), QMetaType::Double); |
| 58 |
} |
| 59 |
|
| 60 |
//editorFactory has been deleted, so should be creator |
| 61 |
//because editorFActory has the ownership |
| 62 |
QVERIFY(creator.isNull()); |
| 63 |
QVERIFY(creator2.isNull()); |
| 64 |
|
| 65 |
delete creator; |
| 66 |
} |
| 67 |
|
| 68 |
void tst_QItemEditorFactory::uintValues() |
| 69 |
{ |
| 70 |
QItemEditorFactory editorFactory; |
| 71 |
|
| 72 |
QWidget parent; |
| 73 |
|
| 74 |
{ |
| 75 |
QWidget *editor = editorFactory.createEditor(QMetaType::UInt, &parent); |
| 76 |
QCOMPARE(editor->metaObject()->className(), "QUIntSpinBox"); |
| 77 |
QCOMPARE(editor->metaObject()->userProperty().userType(), QMetaType::UInt); |
| 78 |
} |
| 79 |
{ |
| 80 |
QWidget *editor = editorFactory.createEditor(QMetaType::Int, &parent); |
| 81 |
QCOMPARE(editor->metaObject()->className(), "QSpinBox"); |
| 82 |
QCOMPARE(editor->metaObject()->userProperty().userType(), QMetaType::Int); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
QTEST_MAIN(tst_QItemEditorFactory) |
| 87 |
#include "tst_qitemeditorfactory.moc" |
| 88 |
|