简体   繁体   English

如何将数据从C ++“拉”到qml?

[英]How to “pull” data from c++ to qml?

I want "pull"data from c++ in qml like this: 我想要像这样从Q ++中的c ++“拉”数据:

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

When MySettings registered in the following way: 当MySettings以以下方式注册时:

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& qrc:/GUI/App.qml:35:错误:未知方法参数类型:int&

So how correctly "pull" data inside qml from c++? 那么如何从C ++正确地“提取” qml中的数据呢?

UPDATE : 更新

I explain better. 我解释得更好。 Now I can call the c++ function (and send params) from qml: 现在我可以从qml调用c ++函数(并发送参数):

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

In c++ code I receive function call with params values "111" and "222". 在C ++代码中,我收到参数值为“ 111”和“ 222”的函数调用。

But I want change this parameters in c++. 但是我想在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". 我想在C ++代码参数中设置“ 333”和“ 555”。 So after call MySettings.someFunc(a, b) I expected that (a==333) and (b==555). 因此,在调用MySettings.someFunc(a,b)之后,我期望(a == 333)和(b == 555)。

How to do this? 这个怎么做?

Don't try to get the return values as reference parameters when calling C++ functions from QML. 从QML调用C ++函数时,请勿尝试获取返回值作为参考参数。 Instead, use return values. 而是使用返回值。 To transfer more than one value in a single call, define your C++ method like 要在一个调用中传递多个值,请定义您的C ++方法,例如

Q_INVOKABLE QVariantList someFunc() { ... }

and use it in QML via 并通过在QML中使用它

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

Passing values ​​by reference do not work calling c ++ functions from QML. 通过引用传递值无法从QML调用c ++函数。 If you want sync calls, use something link this in your c ++ code: 如果要同步调用,请在c ++代码中使用以下链接:

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: 在您的QML代码中是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM