summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2009-07-02 00:48:50 (GMT)
committerAlan Alpert <alan.alpert@nokia.com>2009-07-02 00:48:50 (GMT)
commitafe57350c40ea2469413e4c5b42a7fd45f55769f (patch)
tree45b4de413df72e30513416538a52ae9889aa9a50
parent4e9336d1f60089bffda7fca93a63d18ffce0a6fa (diff)
downloadQt-afe57350c40ea2469413e4c5b42a7fd45f55769f.zip
Qt-afe57350c40ea2469413e4c5b42a7fd45f55769f.tar.gz
Qt-afe57350c40ea2469413e4c5b42a7fd45f55769f.tar.bz2
Beautification of dynamic object example.
Adds some error handling, and cleans up documentation and the example a little.
-rw-r--r--examples/declarative/dynamic/dynamic.js43
-rw-r--r--examples/declarative/dynamic/dynamic.qml25
-rw-r--r--src/declarative/qml/qmlcomponent.cpp23
-rw-r--r--src/declarative/qml/qmlcomponent.h1
-rw-r--r--src/declarative/qml/qmlengine.cpp7
5 files changed, 72 insertions, 27 deletions
diff --git a/examples/declarative/dynamic/dynamic.js b/examples/declarative/dynamic/dynamic.js
index 66ec292..6b483fd 100644
--- a/examples/declarative/dynamic/dynamic.js
+++ b/examples/declarative/dynamic/dynamic.js
@@ -1,51 +1,54 @@
-var sprite = null;
+var dynamicObject = null;
+var fourthBox = null;
var component;
var started = false;
-function make(p) {
+function createWithEvalQml(p) {
return evalQml('Rect { color: "lightsteelblue"; width: 100;'
+ 'height: 100; id: newRect}','DynPart.qml');
}
-function death() {
- if(!(sprite==null)){
- sprite.destroy();
- sprite = null;
+function destroyDynamicObject() {
+ if(!(dynamicObject==null)){
+ dynamicObject.destroy();
+ dynamicObject = null;
}
}
-function spawn() {//Like create, but assumes instant readyness
- if(sprite!=null)//Already made
+function instantCreateWithComponent() {//Like create, but assumes instant readyness
+ if(dynamicObject!=null)//Already made
return null;
component = createComponent("dynamic.qml");
- sprite = component.createObject();
- if(sprite == null){
- print("err");
+ dynamicObject = component.createObject();
+ if(dynamicObject == null){
+ print("error creating component");
}else{
- sprite.parent = targetItem;
- return sprite;
+ dynamicObject.parent = targetItem;
+ return dynamicObject;
}
return null;
}
function finishCreation(){
if(component.isReady()){
- sprite = component.createObject();
- sprite.parent = targetItem;
+ dynamicObject = component.createObject();
+ dynamicObject.parent = targetItem;
}else if(component.isError()){
- sprite = null;
+ dynamicObject = null;
+ print("error creating component");
+ print(component.errorsString());
}
}
-function create(){
+function createWithComponent(){
if(started!=false){
finishCreation();//Remakes if destroyed
- return sprite;
+ return dynamicObject;
}
started = true;
component = createComponent("dynamic.qml");
finishCreation();
- if(sprite != null){
- return sprite;
+ if(dynamicObject != null){
+ return dynamicObject;
}
component.statusChanged.connect(finishCreation);
return null;
diff --git a/examples/declarative/dynamic/dynamic.qml b/examples/declarative/dynamic/dynamic.qml
index ee81ff6..3e0c12e 100644
--- a/examples/declarative/dynamic/dynamic.qml
+++ b/examples/declarative/dynamic/dynamic.qml
@@ -1,16 +1,31 @@
Rect { id: page; width: 800; height: 800; color:"black"
Script { source: "dynamic.js" }
- property bool fourthBox: false;
+ property bool extendStars: false;
Item { id: targetItem; x: 100; y: 100; }
Item { id: targetItem2; x: 0; y: 300; }
Rect { width: 100; height: 100; color: "green"; id: rect
- MouseRegion { anchors.fill:parent; onClicked: {a = create();}}
+ MouseRegion { anchors.fill:parent; onClicked: {a = createWithComponent();}}
}
Rect { width: 100; height: 100; color: "red"; id: rect2; y:100;
- MouseRegion { anchors.fill:parent; onClicked: {death();}}
+ MouseRegion { anchors.fill:parent; onClicked: {destroyDynamicObject();}}
}
Rect { width: 100; height: 100; color: "blue"; id: rect3; y:200;
- MouseRegion { anchors.fill:parent; onClicked: {a = make(); if(a!=null){a.parent = targetItem2; fourthBox = true;}}}
+ MouseRegion { anchors.fill:parent; onClicked:
+ {
+ if(fourthBox == null) {
+ a = createWithEvalQml();
+ if(a!=null) {
+ a.parent = targetItem2;
+ fourthBox = a;
+ extendStars = true;
+ }
+ } else {
+ fourthBox.destroy();
+ fourthBox = null;
+ extendStars = false;
+ }
+ }
+ }
}
- Particles { x:0; y:0; count:20; lifeSpan:500; width:100; height: if(fourthBox){400;}else{300;} source:"star.png"}
+ Particles { x:0; y:0; count:20; lifeSpan:500; width:100; height: if(extendStars){400;}else{300;} source:"star.png"}
}
diff --git a/src/declarative/qml/qmlcomponent.cpp b/src/declarative/qml/qmlcomponent.cpp
index 1144639..293082f 100644
--- a/src/declarative/qml/qmlcomponent.cpp
+++ b/src/declarative/qml/qmlcomponent.cpp
@@ -372,6 +372,29 @@ QList<QmlError> QmlComponent::errors() const
}
/*!
+ Return the list of errors that occured during the last compile or create
+ operation, as a single string. An empty string is returned if isError()
+ is not set.
+
+ This function is similar to errors(), except more useful when called from
+ QML. C++ code should usually use errors().
+
+ \sa errors()
+*/
+QString QmlComponent::errorsString() const
+{
+ Q_D(const QmlComponent);
+ QString ret;
+ if(!isError())
+ return ret;
+ foreach(const QmlError &e, d->errors){
+ ret += e.url().toString() + ":" + QString::number(e.line()) + " "
+ + e.description() + "\n";
+ }
+ return ret;
+}
+
+/*!
Return the component URL. This is the URL passed to either the constructor,
or the loadUrl() or setData() methods.
*/
diff --git a/src/declarative/qml/qmlcomponent.h b/src/declarative/qml/qmlcomponent.h
index db34e16..b29c123 100644
--- a/src/declarative/qml/qmlcomponent.h
+++ b/src/declarative/qml/qmlcomponent.h
@@ -82,6 +82,7 @@ public:
Q_INVOKABLE bool isLoading() const;
QList<QmlError> errors() const;
+ Q_INVOKABLE QString errorsString() const;
QUrl url() const;
diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp
index fc2c7e3..8921e0c 100644
--- a/src/declarative/qml/qmlengine.cpp
+++ b/src/declarative/qml/qmlengine.cpp
@@ -859,6 +859,7 @@ QScriptValue QmlEngine::qmlScriptObject(QObject* object, QmlEngine* engine)
sprite = component.createObject();
if(sprite == 0){
// Error Handling
+ print(component.errorsString());
}else{
sprite.parent = page;
sprite.x = 200;
@@ -866,8 +867,8 @@ QScriptValue QmlEngine::qmlScriptObject(QObject* object, QmlEngine* engine)
}
\endcode
- If you want to just create an arbitrary string of QML, instead of an
- existing qml component or qml file, consider the evalQML() function.
+ If you want to just create an arbitrary string of QML, instead of
+ loading a qml file, consider the evalQML() function.
\sa QmlComponent::createObject(), QmlEngine::createQMLObject()
*/
QScriptValue QmlEngine::createComponent(QScriptContext *ctxt, QScriptEngine *engine)
@@ -878,6 +879,8 @@ QScriptValue QmlEngine::createComponent(QScriptContext *ctxt, QScriptEngine *eng
if(ctxt->argumentCount() != 1 || !activeEngine){
c = new QmlComponent(activeEngine);
}else{
+ //### This url needs to be resolved in the context that the function
+ //### is called - it can't be done here.
QUrl url = QUrl(ctxt->argument(0).toString());
c = new QmlComponent(activeEngine, url, activeEngine);
}