Basti An
Basti An

Reputation: 387

QML dynamic object pass attributes

How can I pass through javascript attributes to a qml object that is not a text or a number. I want to pass the attribute to a value of a c++ property that sets the text of the attribute.

var component;
var gauge;

function createVerticalGauge(setWidth,setX,setY,setID,setText,setValue) {
    component = Qt.createComponent("verticalbargauge.qml");
    console.log(component.status)
    if (component.status == Component.Ready)
        finishCreation(setWidth,setX,setY,setID,setText,setValue);
    else
        component.statusChanged.connect(finishCreation);
}

function finishCreation(setWidth,setX,setY,setID,setText,setValue) {
    if (component.status == Component.Ready) {
        gauge = component.createObject(adaptronicDash, {"id": setID, "gaugetext": setValue,
                                           "x": setX, "y": setY});
        gauge.width = setWidth;
        if (gauge == null) {
            // Error Handling
            console.log("Error creating object");
        }
    } else if (component.status == Component.Error) {
        // Error Handling
        console.log("Error loading component:", component.errorString());
    }
}

The JS is called with this call:

Component.onCompleted: CreateVerticalGaugeScript.createVerticalGauge(300,10,300,"map","MAP",Dashboard.MAP);

Dashboard.MAP sets the Text property of the object. Whem the object is created in the QML file directly, the text gets set with Dashboard.MAP, but witht the script its just "0".

Dashboard.MAP is a qreal which comes from the class Dashboard that provides the values for my qml objects:

Q_PROPERTY(qreal MAP READ MAP WRITE setMAP NOTIFY MAPChanged)

How can i pass Dashboard.MAP with javascript. In my example setValue has the value qml: undefined

Upvotes: 0

Views: 630

Answers (1)

dtech
dtech

Reputation: 49319

Instead of passing Dashboard.MAP only pass the Dashboard object reference to the function.

Then, when you create the attribute object, use this format:

"gaugetext": Qt.binding(function(){return setValue.MAP})

(remember, setValue is Dashboard)

This way gaugetext will be bound to MAP and will change with it.

Upvotes: 1

Related Questions