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

问题描述 投票:0回答:2

我希望qml中的c ++“拉”数据如下:

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

当MySettings以下列方式注册时:

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

但是当我像这样制作函数签名时:

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

我收到以下错误:

qrc:/GUI/App.qml:35:错误:未知方法参数类型:int&

那么如何从c ++中正确地“拉”qml中的数据呢?

更新:

我解释得更好。现在我可以从qml调用c ++函数(并发送params):

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

在c ++代码中,我接收带有参数值“111”和“222”的函数调用。

但我想在c ++中更改此参数。我想要那样的smth:

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

我想在c ++代码中设置params到“333”和“555”。所以在调用MySettings.someFunc(a,b)之后,我期望(a == 333)和(b == 555)。

这该怎么做?

c++ qt qml qt5 qqmlcomponent
2个回答
1
投票

从QML调用C ++函数时,不要尝试将返回值作为引用参数。而是使用返回值。要在一次调用中传输多个值,请定义您的C ++方法

Q_INVOKABLE QVariantList someFunc() { ... }

并在QML中使用它

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

1
投票

通过引用传递值不适用于从QML调用c ++函数。如果您想要同步调用,请在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;
    }

在你的QML代码中有这样的东西:

var test = gapi.someFunc(3,2); // pass values here and get the new ones
console.log("The return data" + test);
© www.soinside.com 2019 - 2024. All rights reserved.