rwik
rwik

Reputation: 785

How to copy an instance of element while dragging in qml?

I am using the following code to drag. But it actually moves the original element. Instead i require a copied object to be dragged , which wil be destroyed on drop . drag.active: dragarea.drag.active Mousearea{ Id : dragarea Anchors.fill : parent Drag.target: parent }

Upvotes: 1

Views: 2129

Answers (1)

Mitch
Mitch

Reputation: 24386

Read Dynamic QML Object Creation from JavaScript. It will describe two options:

Creating a Component Dynamically

To dynamically load a component defined in a QML file, call the Qt.createComponent() function in the Qt object. This function takes the URL of the QML file as its only argument and creates a Component object from this URL.

Once you have a Component, you can call its createObject() method to create an instance of the component.

Creating an Object from a String of QML

If the QML is not defined until runtime, you can create a QML object from a string of QML using the Qt.createQmlObject() function

That documentation also has information on how to delete the objects when you're done with them.

You can also use a Loader:

Loader is used to dynamically load QML components.

Loader can load a QML file (using the source property) or a Component object (using the sourceComponent property). It is useful for delaying the creation of a component until it is required: for example, when a component should be created on demand, or when a component should not be created unnecessarily for performance reasons.

Again, destruction of the dynamically loaded item is documented there, too.

Upvotes: 1

Related Questions