summaryrefslogtreecommitdiffstats
path: root/examples/declarative/dynamic
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2009-06-09 07:22:25 (GMT)
committerAlan Alpert <alan.alpert@nokia.com>2009-06-09 07:22:25 (GMT)
commit82cff1af638de427aa3d6f8bd147f4451662ee65 (patch)
treed28dd7ef57a4b07088821aea4d1cc0b22d3a2d5b /examples/declarative/dynamic
parent1ef0d84c8e5d706e60e487fc801f90bbf5fddaa6 (diff)
downloadQt-82cff1af638de427aa3d6f8bd147f4451662ee65.zip
Qt-82cff1af638de427aa3d6f8bd147f4451662ee65.tar.gz
Qt-82cff1af638de427aa3d6f8bd147f4451662ee65.tar.bz2
Add dynamic object creation example
Diffstat (limited to 'examples/declarative/dynamic')
-rw-r--r--examples/declarative/dynamic/dynamic.js52
-rw-r--r--examples/declarative/dynamic/dynamic.qml16
-rw-r--r--examples/declarative/dynamic/star.pngbin0 -> 262 bytes
3 files changed, 68 insertions, 0 deletions
diff --git a/examples/declarative/dynamic/dynamic.js b/examples/declarative/dynamic/dynamic.js
new file mode 100644
index 0000000..66ec292
--- /dev/null
+++ b/examples/declarative/dynamic/dynamic.js
@@ -0,0 +1,52 @@
+var sprite = null;
+var component;
+var started = false;
+function make(p) {
+ return evalQml('Rect { color: "lightsteelblue"; width: 100;'
+ + 'height: 100; id: newRect}','DynPart.qml');
+}
+
+function death() {
+ if(!(sprite==null)){
+ sprite.destroy();
+ sprite = null;
+ }
+}
+
+function spawn() {//Like create, but assumes instant readyness
+ if(sprite!=null)//Already made
+ return null;
+ component = createComponent("dynamic.qml");
+ sprite = component.createObject();
+ if(sprite == null){
+ print("err");
+ }else{
+ sprite.parent = targetItem;
+ return sprite;
+ }
+ return null;
+}
+
+function finishCreation(){
+ if(component.isReady()){
+ sprite = component.createObject();
+ sprite.parent = targetItem;
+ }else if(component.isError()){
+ sprite = null;
+ }
+}
+
+function create(){
+ if(started!=false){
+ finishCreation();//Remakes if destroyed
+ return sprite;
+ }
+ started = true;
+ component = createComponent("dynamic.qml");
+ finishCreation();
+ if(sprite != null){
+ return sprite;
+ }
+ component.statusChanged.connect(finishCreation);
+ return null;
+}
diff --git a/examples/declarative/dynamic/dynamic.qml b/examples/declarative/dynamic/dynamic.qml
new file mode 100644
index 0000000..ee81ff6
--- /dev/null
+++ b/examples/declarative/dynamic/dynamic.qml
@@ -0,0 +1,16 @@
+Rect { id: page; width: 800; height: 800; color:"black"
+ Script { source: "dynamic.js" }
+ property bool fourthBox: 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();}}
+ }
+ Rect { width: 100; height: 100; color: "red"; id: rect2; y:100;
+ MouseRegion { anchors.fill:parent; onClicked: {death();}}
+ }
+ 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;}}}
+ }
+ Particles { x:0; y:0; count:20; lifeSpan:500; width:100; height: if(fourthBox){400;}else{300;} source:"star.png"}
+}
diff --git a/examples/declarative/dynamic/star.png b/examples/declarative/dynamic/star.png
new file mode 100644
index 0000000..defbde5
--- /dev/null
+++ b/examples/declarative/dynamic/star.png
Binary files differ