aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/declarative
diff options
context:
space:
mode:
authorChris Adams <christopher.adams@nokia.com>2011-10-10 12:11:21 +1000
committerQt by Nokia <qt-info@nokia.com>2011-12-06 02:46:28 +0100
commit564d58025e46c825ec763193767defd78f5e13a0 (patch)
tree31d046ac0c016df416afd4923fb749d8bf73d6bc /doc/src/declarative
parent8601f3444f7926a7b47ae217d8bf51044ff61809 (diff)
Ensure that scarce resources work with var properties
Now that we have a new property type which stores JavaScript handles, we need to ensure that scarce resources can be used with them. Task-number: QMLNG-18 Task-number: QTBUG-21843 Change-Id: I4a920ae39e7d33cf5e33362e5e0ee21c74cb35e3 Reviewed-by: Martin Jones <martin.jones@nokia.com>
Diffstat (limited to 'doc/src/declarative')
-rw-r--r--doc/src/declarative/basictypes.qdoc13
-rw-r--r--doc/src/declarative/javascriptblocks.qdoc172
2 files changed, 49 insertions, 136 deletions
diff --git a/doc/src/declarative/basictypes.qdoc b/doc/src/declarative/basictypes.qdoc
index 98eea78a07..a908e73ca8 100644
--- a/doc/src/declarative/basictypes.qdoc
+++ b/doc/src/declarative/basictypes.qdoc
@@ -479,6 +479,19 @@
}
\endqml
+ A \c var type property can also hold an image or pixmap.
+ A \c var which contains a QPixmap or QImage is known as a
+ "scarce resource" and the declarative engine will attempt to
+ automatically release such resources after evaluation of any JavaScript
+ expression which requires one to be copied has completed.
+
+ Clients may explicitly release such a scarce resource by calling the
+ "destroy" method on the \c var property from within JavaScript. They
+ may also explicitly preserve the scarce resource by calling the
+ "preserve" method on the \c var property from within JavaScript.
+ For more information regarding the usage of a scarce resource, please
+ see \l{Scarce Resources in JavaScript}.
+
\sa {QML Basic Types}
*/
diff --git a/doc/src/declarative/javascriptblocks.qdoc b/doc/src/declarative/javascriptblocks.qdoc
index a9e383c258..34a1986db6 100644
--- a/doc/src/declarative/javascriptblocks.qdoc
+++ b/doc/src/declarative/javascriptblocks.qdoc
@@ -362,7 +362,7 @@ Item {
\section1 Scarce Resources in JavaScript
-As described in the documentation for \l{QML Basic Types}, a \c variant type
+As described in the documentation for \l{QML Basic Types}, a \c var type
property may hold a "scarce resource" (image or pixmap). There are several
important semantics of scarce resources which should be noted:
@@ -379,30 +379,12 @@ may be necessary for clients to manually preserve or destroy resources for
themselves.
For the following examples, imagine that we have defined the following class:
-\code
-class AvatarExample : public QObject
-{
- Q_OBJECT
- Q_PROPERTY(QPixmap avatar READ avatar WRITE setAvatar NOTIFY avatarChanged)
-public:
- AvatarExample(QObject *parent = 0) : QObject(parent), m_value(100, 100) { m_value.fill(Qt::blue); }
- ~AvatarExample() {}
-
- QPixmap avatar() const { return m_value; }
- void setAvatar(QPixmap v) { m_value = v; emit avatarChanged(); }
-
-signals:
- void avatarChanged();
-
-private:
- QPixmap m_value;
-};
-\endcode
+
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/avatarExample.h 0
and that we have registered it with the QML type-system as follows:
-\code
-qmlRegisterType<AvatarExample>("Qt.example", 1, 0, "AvatarExample");
-\endcode
+
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/avatarExample.cpp 0
The AvatarExample class has a property which is a pixmap. When the property
is accessed in JavaScript scope, a copy of the resource will be created and
@@ -414,141 +396,59 @@ unless the client explicitly preserves it.
\section2 Example One: Automatic Release
-In this example, the resource will be automatically
+In the following example, the scarce resource will be automatically released
+after the binding evaluation is complete.
+
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/exampleOne.qml 0
+
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/avatarExample.cpp 1
+
+\section2 Example Two: Automatic Release Prevented By Reference
+
+In this example, the resource will not be automatically
released after the binding expression evaluation is
-complete.
+complete, because there is a property var referencing the
+scarce resource.
-\qml
-// exampleOne.qml
-import QtQuick 1.0
-import Qt.example 1.0
-
-QtObject {
- property AvatarExample a;
- a: AvatarExample { id: example }
- property variant avatar: example.avatar
-}
-\endqml
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/exampleTwo.qml 0
-\code
-QDeclarativeComponent component(&engine, "exampleOne.qml");
-QObject *object = component.create();
-// The scarce resource will have been released automatically
-// after the binding expression was evaluated.
-// Since the scarce resource was not released explicitly prior
-// to the binding expression being evaluated, we get the
-// expected result:
-//object->property("scarceResourceCopy").isValid() == true
-delete object;
-\endcode
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/avatarExample.cpp 2
-\section2 Example Two: Explicit Preservation
+\section2 Example Three: Explicit Preservation
In this example, the resource must be explicitly preserved in order
to prevent the declarative engine from automatically releasing the
resource after evaluation of the imported script.
-\code
-// exampleTwo.js
-.import Qt.example 1.0 as QtExample
-
-var component = Qt.createComponent("exampleOne.qml");
-var exampleOneElement = component.createObject(null);
-var avatarExample = exampleOneElement.a;
-var retn = avatarExample.avatar;
-
-// without the following call, the scarce resource held
-// by retn would be automatically released by the engine
-// after the import statement in exampleTwo.qml, prior
-// to the variable assignment.
-retn.preserve();
-
-function importAvatar() {
- return retn;
-}
-\endcode
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/exampleThree.js 0
-\qml
-// exampleTwo.qml
-import QtQuick 1.0
-import Qt.example 1.0
-import "exampleTwo.js" as ExampleTwoJs
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/exampleThree.qml 0
-QtObject {
- property variant avatar: ExampleTwoJs.importAvatar()
-}
-\endqml
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/avatarExample.cpp 3
-\code
-QDeclarativeComponent component(&engine, "exampleTwo.qml");
-QObject *object = component.create();
-// The resource was preserved explicitly during evaluation of the
-// JavaScript expression. Thus, during property assignment, the
-// scarce resource was still valid, and so we get the expected result:
-//object->property("avatar").isValid() == true
-// The scarce resource may not have been cleaned up by the JS GC yet;
-// it will continue to consume system resources until the JS GC runs.
-delete object;
-\endcode
-
-\section2 Example Three: Explicit Destruction
+\section2 Example Four: Explicit Destruction
In the following example, we release (via destroy()) an explicitly preserved
scarce resource variant. This example shows how a client may free system
resources by releasing the scarce resource held in a JavaScript object, if
required, during evaluation of a JavaScript expression.
-\code
-// exampleThree.js
-.import Qt.example 1.0 as QtExample
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/exampleFour.js 0
-var component = Qt.createComponent("exampleOne.qml");
-var exampleOneElement = component.createObject(null);
-var avatarExample = exampleOneElement.a;
-var retn = avatarExample.avatar;
-retn.preserve();
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/exampleFour.qml 0
-function importAvatar() {
- return retn;
-}
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/avatarExample.cpp 4
-function releaseAvatar() {
- retn.destroy();
-}
-\endcode
+\section2 Example Five: Explicit Destruction And JavaScript References
-\qml
-// exampleThree.qml
-import QtQuick 1.0
-import Qt.example 1.0
-import "exampleThree.js" as ExampleThreeJs
-
-QtObject {
- property variant avatarOne
- property variant avatarTwo
-
- Component.onCompleted: {
- avatarOne = ExampleThreeJs.importAvatar(); // valid at this stage
- ExampleThreeJs.releaseAvatar(); // explicit release
- avatarTwo = ExampleThreeJs.importAvatar(); // invalid at this stage
- }
-}
-\endqml
+One thing to be aware of when using "var" type properties is that they
+hold references to JavaScript objects. As such, if multiple references
+to one scarce resource is held, and the client calls destroy() on one
+of those references (to explicitly release the scarce resource), all of
+the references will be affected.
-\code
-QDeclarativeComponent component(&engine, "exampleThree.qml");
-QObject *object = component.create();
-// The scarce resource was explicitly preserved by the client during
-// the evaluation of the imported script, and so the scarce resource
-// remains valid until the explicit call to releaseAvatar(). As such,
-// we get the expected results:
-//object->property("avatarOne").isValid() == true
-//object->property("avatarTwo").isValid() == false
-// Because the scarce resource was released explicitly, it will no longer
-// be consuming any system resources (beyond what a normal JS Object would;
-// that small overhead will exist until the JS GC runs, as per any other
-// JavaScript object).
-delete object;
-\endcode
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/exampleFive.qml 0
+
+\snippet doc/src/snippets/declarative/integrating-javascript/scarceresources/avatarExample.cpp 5
*/