Adding a MouseArea and/or signal handler to an object created through Component.createObject()


  • Moderators

    Hi all,

    There seems to be 2 ways of creating Qt Quick items dynamically, as described in http://qt-project.org/doc/qt-5/qtqml-javascript-dynamicobjectcreation.html .

    I'm currently creating new copies of a QML object on demand by building a QML string and calling Qt.createQmlObject(). This works well, but the string building code is a bit messy to maintain.

    I've started investigating Qt.createComponent() + Component.createObject(), to see if I can achieve the same result using tidier code. If mostly works, but I haven't figured out one thing: How do I add a MouseArea and/or signal handler to an object created this way?

    I thought of creating a MouseArea dynamically using Qt.createComponent("MouseArea.qml") and then setting my new component as the parent, but MouseArea.qml doesn't exist.

    So, I added I embedded the mouse area into the custom component's .qml file for now. I can't capture its events though:
    @
    function saySomething() {
    console.debug("Whee")
    }

    ...

    var component = Qt.createComponent("MyDraggableItem.qml")
    component.createObject(root, {
    "x": 50,
    "y": 50,
    "onXChanged": saySomething // WRONG
    })
    @

    The new object appears and can be dragged around the screen, but my saySomething() function doesn't get called when I drag it (I wasn't expecting this to work though, since "onXChanged" isn't a property).

    Thanks in advance!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.