Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Adding a MouseArea and/or signal handler to an object created through Component.createObject()

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

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 922 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on last edited by
    #1

    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!

    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved