itemCreation.js Example Filedeclarative/toys/dynamicscene/qml/itemCreation.jsvar itemComponent = null; var draggedItem = null; var startingMouse; var posnInWindow; function startDrag(mouse) { posnInWindow = paletteItem.mapToItem(window, 0, 0); startingMouse = { x: mouse.x, y: mouse.y } loadComponent(); } //Creation is split into two functions due to an asynchronous wait while //possible external files are loaded. function loadComponent() { if (itemComponent != null) { // component has been previously loaded createItem(); return; } itemComponent = Qt.createComponent(paletteItem.componentFile); if (itemComponent.status == Component.Loading) //Depending on the content, it can be ready or error immediately component.statusChanged.connect(createItem); else createItem(); } function createItem() { if (itemComponent.status == Component.Ready && draggedItem == null) { draggedItem = itemComponent.createObject(window, {"image": paletteItem.image, "x": posnInWindow.x, "y": posnInWindow.y, "z": 3}); // make sure created item is above the ground layer } else if (itemComponent.status == Component.Error) { draggedItem = null; console.log("error creating component"); console.log(itemComponent.errorString()); } } function continueDrag(mouse) { if (draggedItem == null) return; draggedItem.x = mouse.x + posnInWindow.x - startingMouse.x; draggedItem.y = mouse.y + posnInWindow.y - startingMouse.y; } function endDrag(mouse) { if (draggedItem == null) return; if (draggedItem.x + draggedItem.width > toolbox.x) { //Don't drop it in the toolbox draggedItem.destroy(); draggedItem = null; } else { draggedItem.created = true; draggedItem = null; } } © 2008-2011 Nokia Corporation and/or its subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. Privacy Policy Licensees holding valid Qt Commercial licenses may use this document in accordance with the Qt Commercial License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and Nokia. Alternatively, this document may be used under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. |