Qt World Summit: Submit your Presentation

[SOLVED]Dynamically Created Custom QML Type has no Children

  • I've written a custom QML Type in c++ which is a Vehicle Gauge. The class works wonderfully and I can export it to QML and use it just fine. I then created a QML file with this custom type as the Parent/Root item in the file. If I load this QML file in start up everything works as desired. My custom signals work as I expect them to. The other objects (ie MouseArea, Rectangle) behave just fine.

    The problem comes when I try to dynamically create this QML file. None of the objects know anything about the other ones in the file and my signals form the custom QML type don't fire. I know they are getting sent but the QML doesn't do anything. The object loads and shows up in the view but dosn't do anything.

    Here is some Code to demonstrate a little bit:

    @class MyGauge: public QQuickPaintedItem

    //QML properties
    //various properties


    void mySignal();
    // functions and such

    QML file using MyGauge:
    id: gauge
    onMySignal: {
    console.log("on move gauge")

    Rectangle {
        id: highlight
        anchors.fill: parent
        color: "transparent"
        border.color: "black"
        border.width: 3
        visible: mouse.visible
    MouseArea {
        id: mouse
        anchors.fill: parent
        visible: false
        onReleased: {
            gauge.z = 0//bottom level
            drag.target = null
            visible = false
        function function() {
            visible = true
            drag.target = gauge
            drag.axis = Drag.XandYAxis
            drag.maximumX = gauge.parent.width - gauge.width
            drag.minimumX = 0
            drag.maximumY = gauge.parent.height - gauge.height
            drag.minimumY = 0
            gauge.z = 31//on top of all gauges
    function function1(col,row) {
    function function2(x,y,mX,mY) {


    javascript for creating MyGauge:

    function createGauge(string, row, col, parent, color, redline, needle, pid) {
    console.log(string, parent)
    component = Qt.createComponent(string);
    if (component.status === Component.Ready) {
    gauge = component.createObject(parent, {"y": col160, "x": row80, "color": color, "needle": needle, "redline": redline, "parameter": pid});
    if (gauge === null) {
    console.log("Error creating object");
    } else if (component.status === Component.Error) {
    console.log("Error loading component:", component.errorString());

    As I said before the QML file works perfectly when it is not used with the javascript to dynamically create the object. But when I use the javascript to create the object onMySignal doesn't even process. I can manually connect the signal to a function using :

    However then I can't call function1 or function2 anywhere in the file because I get then error that they are undefined. The highlight.visible also does not update with the mouse.visible even if I use a Binding element. Also when i try to look at the children of gauge i get null...as if the MouseArea and Rectangle aren't event there.

    I'm completely at a loss as to what is happening...at this point any advice would be better than my frustrated attempts to hack this apart.

  • OK I ended up solving this one on my own...

    The problem was where is was calling the javascript function createGauge.

    Basically I had a Parent object that dynamically created a menu for the gauge selection...I called my create function within the menu and it did not work.

    When I create MyGauge from within the parent object it worked just fine.

    Thanks. I hope that made sense...if you have any questions of that didn't make any sense I'd be happy to clarify.

Log in to reply