AeroSun
AeroSun

Reputation: 2571

How to "pull" data from c++ to qml?

I want "pull"data from c++ in qml like this:

   Component.onCompleted: {
        MySettings.loadMainWindowPosition(aAppWnd.x, aAppWnd.y, aAppWnd.width, aAppWnd.height, aAppWnd.visibility);
    }

When MySettings registered in the following way:

context->setContextProperty("MySettings", m_settings);

But when I make the funtion signature like this:

void MySettings::loadMainWindowPosition(int& x, int& y, int& width, int& height, int& visibility)

I received the following error:

qrc:/GUI/App.qml:35: Error: Unknown method parameter type: int&

So how correctly "pull" data inside qml from c++?

UPDATE:

I explain better. Now I can call the c++ function (and send params) from qml:

   Component.onCompleted: {
        MySettings.someFunc(111, 222);
    }

In c++ code I receive function call with params values "111" and "222".

But I want change this parameters in c++. I want smth like that:

   Component.onCompleted: {
        var a;
        var b;
        MySettings.someFunc(a, b);
    }

I want set up in the c++ code params to the "333" and "555". So after call MySettings.someFunc(a, b) I expected that (a==333) and (b==555).

How to do this?

Upvotes: 0

Views: 1307

Answers (2)

Adriano Campos
Adriano Campos

Reputation: 1199

Passing values ​​by reference do not work calling c ++ functions from QML. If you want sync calls, use something link this in your c ++ code:

QVariantList MySettings::someFunc(int a, int b){

        QVariantList list;
        list.append(a + 5); // edit the passed values here
        list.append(b + 5); // edit the passed values here
        return list;
    }

and something like this in your QML code:

var test = gapi.someFunc(3,2); // pass values here and get the new ones
console.log("The return data" + test);

Upvotes: 1

Mitja
Mitja

Reputation: 2011

Don't try to get the return values as reference parameters when calling C++ functions from QML. Instead, use return values. To transfer more than one value in a single call, define your C++ method like

Q_INVOKABLE QVariantList someFunc() { ... }

and use it in QML via

Component.onCompleted: {
    var returnValues = MySettings.someFunc();
    //access the returnValues via list indices here:
    var a = returnValues[0];
    var b = returnValues[1];
}

Upvotes: 3

Related Questions