首页 > Qml 与 Qml , Qml 与 C++ 的值共享

Qml 与 Qml , Qml 与 C++ 的值共享

例如,在 文件1.qml 里定义了一个 TextInput

TextInput {
    id: input1
    ...
}

然后有一个 文件2.qml ,如何在 文件2.qml 中获取 文件1.qml 中的 TextInput.text

同样的,如果有一个 文件3.cpp ,如何在 文件3.cpp 中获取 文件1.qmpTextInput.text 呢?

先谢谢大家了!


qml使用的是统一的名称空间,只要包含,那么就可以引用其id,新手理解,勿喷~


情况一的问题可以像下面这样解决:

QML1 名称定为 TextInputA.qml

TextInput {
    id: input1
    ...
}

QML2 名称定为 TextInputB.qml

TextInput {
    id: input2
    ...
}

现在你有一个使用这两个类的类,名称为ExampleA.qml

Item{
    TextInputA{
        id: inputA
    }
    
    TextInputB{
        id: inputB
        text: inputA.text
    }
}

情况一的延伸,如果TextInputB被实例化在了一个叫做HelloC.qml的类中该怎么办?HelloC.qml的定义:

HelloC{
    property alias text: inputB.text
    TextInputB{
        id: inputB
        text: inputA.text
    }
}

现在你有一个使用TextInputA和HelloC这两个类的类,名称为ExampleB.qml

Item{
    TextInputA{
        id: inputA
    }
    
    HelloC{
        id: helloC
        text: inputA.text
    }
}

//////////////////////////////////////////////

对于情况二:
你可以在网上搜索 “QML和Qt交互” 来找答案了

希望这个回答可以帮到你和更多人,祝好!


说得有点混乱~

【热门文章】
【热门文章】