[Interest] QML introspection and dynamic component creation

Harri Pasanen harri at mpaja.com
Sat Dec 17 11:04:37 CET 2011


On 12/17/2011 10:15 AM, Bo Thorsen wrote:
>
>> When the user selects Wobble from the list of effects, a Loader is used
>> to create a WobbleEffect item, and the id of this item is assigned to
>> the effect property of the ParameterPanel. What I then want to happen is
>> for sliders to be created and connected [...]
> As you say, this is pretty easy to do with C++ introspection. So why not
> use that? Export a C++ function to QML that does this.
>
> I don't know of a way to do this in QML, but I'd be happy to know. So
> anyone with this knowledge, please step up and describe it.

You can probably do it trough javascript as well.

For instance, I have a Piece.qml that acts  as kind of "class header" to 
create instances of Piece qml objects.

 From javascript I the do:

  var component = Qt.createComponent("Piece.qml");

pieces.push(component.createObject(gameBoard, {"x": xpos, "y": ypos, 
"width": pw, "height": pw}

to setup a gaming board.

To connect the signals, at the end of Piece.qml I have something like:

     Component.onCompleted: {
       mouse.swipe.connect(Ui.movePlayer)

Where mouse.swipe is a signal I created for MouseArea {id: mouse ...} and
where Ui is my javascript, movePlayer being a function in there.

Then to clear the board and remove the objects from screen I do:

     for (var i in pieces)
         pieces[i].destroy()


So to lookup in docs is Qt.createComponent, createObject, connect, destroy.

quickhit example is QtSDK is a full example to play around with.

Hope this helps a bit,

Harri













More information about the Interest mailing list