diff options
-rw-r--r-- | src/qml/doc/src/qmlfunctions.qdoc | 51 | ||||
-rw-r--r-- | src/qml/qml/qqml.h | 18 | ||||
-rw-r--r-- | tests/auto/qml/qqmllanguage/data/singleton/RegisteredCompositeSingletonType.qml | 58 | ||||
-rw-r--r-- | tests/auto/qml/qqmllanguage/data/singletonTest17.qml | 49 | ||||
-rw-r--r-- | tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 16 |
5 files changed, 191 insertions, 1 deletions
diff --git a/src/qml/doc/src/qmlfunctions.qdoc b/src/qml/doc/src/qmlfunctions.qdoc index cd4e86bd31..679f7bac96 100644 --- a/src/qml/doc/src/qmlfunctions.qdoc +++ b/src/qml/doc/src/qmlfunctions.qdoc @@ -51,7 +51,7 @@ \fn void qmlClearTypeRegistrations() \relates QQmlEngine - Clears all stored type registrations, such as those produced with qmlRegisterType(). + Clears all stored type registrations, such as those produced with \l qmlRegisterType. Do not call this function while a QQmlEngine exists or behavior will be undefined. Any existing QQmlEngines must be deleted before calling this function. This function @@ -398,6 +398,55 @@ */ /*! + \fn int qmlRegisterSingletonType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName) + \relates QQmlEngine + + This function may be used to register a singleton type with the name \a qmlName, in the library imported from \a uri having + the version number composed from \a versionMajor and \a versionMinor. The type is defined by the QML file located at \a url. + The url must be an absolute URL, i.e. url.isRelative() == false. + + In addition the type's QML file must have pragma Singleton statement among its import statements. + + A singleton type may be referenced via the type name with which it was registered, and this typename may be used as the + target in a \l Connections type or otherwise used as any other type id would. One exception to this is that a singleton + type property may not be aliased (because the singleton type name does not identify an object within the same component + as any other item). + + Usage: + // First, define your QML singleton type which provides the functionality. + \qml + pragma Singleton + import QtQuick 2.0 + Item { + property int testProp1: 125 + } + \endqml + + \code + // Second, register the QML singleton type by calling this function in an initialization function. + #include <QtQml> + ... + qmlRegisterSingletonType(QUrl("file:///absolute/path/SingletonType.qml"), "Qt.example.qobjectSingleton", 1, 0, "RegisteredSingleton"); + ... + \endcode + + In order to use the registered singleton type in QML, you must import the singleton type. + \qml + import QtQuick 2.0 + import Qt.example.qobjectSingleton 1.0 + Item { + id: root + property int someValue: RegisteredSingleton.testProp1 + } + \endqml + + It is also possible to have QML singleton types registered without using the qmlRegisterSingletonType function. + That can be done by adding a pragma Singleton statement among the imports of the type's QML file. In addition + the type must be defined in a qmldir file with a singleton keyword and the qmldir must be imported by the QML + files using the singleton. +*/ + +/*! \fn int qmlRegisterType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName); \relates QQmlEngine diff --git a/src/qml/qml/qqml.h b/src/qml/qml/qqml.h index 6082fcda08..b6c6fe840d 100644 --- a/src/qml/qml/qqml.h +++ b/src/qml/qml/qqml.h @@ -485,6 +485,24 @@ inline int qmlRegisterSingletonType(const char *uri, int versionMajor, int versi return QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &api); } +inline int qmlRegisterSingletonType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName) +{ + if (url.isRelative()) { + // User input check must go here, because QQmlPrivate::qmlregister is also used internally for composite types + qWarning("qmlRegisterSingletonType requires absolute URLs."); + return 0; + } + + QQmlPrivate::RegisterCompositeSingletonType type = { + url, + uri, + versionMajor, + versionMinor, + qmlName + }; + + return QQmlPrivate::qmlregister(QQmlPrivate::CompositeSingletonRegistration, &type); +} inline int qmlRegisterType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName) { diff --git a/tests/auto/qml/qqmllanguage/data/singleton/RegisteredCompositeSingletonType.qml b/tests/auto/qml/qqmllanguage/data/singleton/RegisteredCompositeSingletonType.qml new file mode 100644 index 0000000000..cdc531ef0d --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/singleton/RegisteredCompositeSingletonType.qml @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BlackBerry Limited. All rights reserved. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +pragma Singleton + +Item { + id: singletonId + + property int testProp1: 925 + property int testProp2: 825 + property int testProp3: 755 + + width: 25; height: 25 + + Rectangle { + id: rectangle + border.color: "white" + anchors.fill: parent + } +}
\ No newline at end of file diff --git a/tests/auto/qml/qqmllanguage/data/singletonTest17.qml b/tests/auto/qml/qqmllanguage/data/singletonTest17.qml new file mode 100644 index 0000000000..655e9d5a78 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/singletonTest17.qml @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BlackBerry Limited. All rights reserved. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import org.qtproject.Test 1.0 + +Item { + id: test + + property int value1: RegisteredSingleton.testProp1; + property string value2: "Test value: " + RegisteredSingleton.testProp3; +} diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index bd66fc88fc..f889036961 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -212,6 +212,7 @@ private slots: void compositeSingletonRemote(); void compositeSingletonJavaScriptPragma(); void compositeSingletonSelectors(); + void compositeSingletonRegistered(); private: QQmlEngine engine; @@ -2847,6 +2848,9 @@ void tst_qqmllanguage::initTestCase() QFile out(testFileUrl(QString::fromUtf8("I18nType\303\201\303\242\303\243\303\244\303\245.qml")).toLocalFile()); QVERIFY2(out.open(QIODevice::WriteOnly), qPrintable(QString::fromLatin1("Cannot open '%1': %2").arg(out.fileName(), out.errorString()))); out.write(in.readAll()); + + // Register a Composite Singleton. + qmlRegisterSingletonType(testFileUrl("singleton/RegisteredCompositeSingletonType.qml"), "org.qtproject.Test", 1, 0, "RegisteredSingleton"); } void tst_qqmllanguage::aliasPropertyChangeSignals() @@ -3521,6 +3525,18 @@ void tst_qqmllanguage::compositeSingletonSelectors() verifyCompositeSingletonPropertyValues(o, "value1", 625, "value2", 455); } +// Reads values from a Singleton that was registered through the C++ API: +// qmlRegisterSingletonType. +void tst_qqmllanguage::compositeSingletonRegistered() +{ + QQmlComponent component(&engine, testFile("singletonTest17.qml")); + VERIFY_ERRORS(0); + QObject *o = component.create(); + QVERIFY(o != 0); + + verifyCompositeSingletonPropertyValues(o, "value1", 925, "value2", 755); +} + QTEST_MAIN(tst_qqmllanguage) #include "tst_qqmllanguage.moc" |