diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-05-14 03:10:22 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-05-14 03:10:22 (GMT) |
commit | 46e7c3f7132aac3838bca4510675f8ad7f70a115 (patch) | |
tree | 34cc8d900c12ad0637bf5f3ac61d8aafee80b366 /tests | |
parent | c8ecc6c73065b0cef11b5d3e3f53af6d9ac60c7d (diff) | |
download | Qt-46e7c3f7132aac3838bca4510675f8ad7f70a115.zip Qt-46e7c3f7132aac3838bca4510675f8ad7f70a115.tar.gz Qt-46e7c3f7132aac3838bca4510675f8ad7f70a115.tar.bz2 |
Custom string converter test
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/declarative/qmlparser/customVariantTypes.txt | 3 | ||||
-rw-r--r-- | tests/auto/declarative/qmlparser/testtypes.h | 20 | ||||
-rw-r--r-- | tests/auto/declarative/qmlparser/tst_qmlparser.cpp | 14 |
3 files changed, 35 insertions, 2 deletions
diff --git a/tests/auto/declarative/qmlparser/customVariantTypes.txt b/tests/auto/declarative/qmlparser/customVariantTypes.txt new file mode 100644 index 0000000..dc5031e --- /dev/null +++ b/tests/auto/declarative/qmlparser/customVariantTypes.txt @@ -0,0 +1,3 @@ +MyQmlObject { + customType: "10" +} diff --git a/tests/auto/declarative/qmlparser/testtypes.h b/tests/auto/declarative/qmlparser/testtypes.h index 190611f..0dc91b2 100644 --- a/tests/auto/declarative/qmlparser/testtypes.h +++ b/tests/auto/declarative/qmlparser/testtypes.h @@ -19,6 +19,20 @@ public: Q_DECLARE_INTERFACE(MyInterface, "com.trolltech.Qt.Test.MyInterface"); QML_DECLARE_INTERFACE(MyInterface); +struct MyCustomVariantType +{ + MyCustomVariantType() : a(0) {} + int a; +}; +Q_DECLARE_METATYPE(MyCustomVariantType); + +static QVariant myCustomVariantTypeConverter(const QString &data) +{ + MyCustomVariantType rv; + rv.a = data.toInt(); + return QVariant::fromValue(rv); +} + class MyQmlObject : public QObject, public MyInterface, public QmlParserStatus { Q_OBJECT @@ -29,9 +43,10 @@ class MyQmlObject : public QObject, public MyInterface, public QmlParserStatus Q_PROPERTY(QMatrix matrix READ matrix WRITE setMatrix) //assumed to be unsupported by QML Q_PROPERTY(MyInterface *interface READ interface WRITE setInterface) Q_PROPERTY(int onLiteralSignal READ onLiteralSignal WRITE setOnLiteralSignal); + Q_PROPERTY(MyCustomVariantType customType READ customType WRITE setCustomType); Q_INTERFACES(MyInterface QmlParserStatus) public: - MyQmlObject() : m_value(-1), m_interface(0) {} + MyQmlObject() : m_value(-1), m_interface(0) { qRegisterMetaType<MyCustomVariantType>("MyCustomVariantType"); } int value() const { return m_value; } void setValue(int v) { m_value = v; } @@ -60,6 +75,8 @@ public: int onLiteralSignal() const { return m_value; } void setOnLiteralSignal(int v) { m_value = v; } + MyCustomVariantType customType() const { return m_custom; } + void setCustomType(const MyCustomVariantType &v) { m_custom = v; } public slots: void basicSlot() { qWarning("MyQmlObject::basicSlot"); } @@ -70,6 +87,7 @@ private: friend class tst_qmlparser; int m_value; MyInterface *m_interface; + MyCustomVariantType m_custom; }; QML_DECLARE_TYPE(MyQmlObject); diff --git a/tests/auto/declarative/qmlparser/tst_qmlparser.cpp b/tests/auto/declarative/qmlparser/tst_qmlparser.cpp index 5af75c4..65ee5e2 100644 --- a/tests/auto/declarative/qmlparser/tst_qmlparser.cpp +++ b/tests/auto/declarative/qmlparser/tst_qmlparser.cpp @@ -9,7 +9,9 @@ class tst_qmlparser : public QObject { Q_OBJECT public: - tst_qmlparser() {} + tst_qmlparser() { + QmlMetaType::registerCustomStringConverter(qMetaTypeId<MyCustomVariantType>(), myCustomVariantTypeConverter); + } private slots: @@ -38,6 +40,7 @@ private slots: void propertyValueSource(); void attachedProperties(); void dynamicObjects(); + void customVariantTypes(); // regression tests for crashes void crash1(); @@ -398,6 +401,15 @@ void tst_qmlparser::dynamicObjects() QVERIFY(object != 0); } +// Tests the registration of custom variant string converters +void tst_qmlparser::customVariantTypes() +{ + QmlComponent component(&engine, TEST_FILE("customVariantTypes.txt")); + MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create()); + QVERIFY(object != 0); + QCOMPARE(object->customType().a, 10); +} + void tst_qmlparser::crash1() { QmlComponent component(&engine, "Component {}"); |