本页内容

Qt Remote Objects QML Types

用于 Qt Remote Objects 提供了构建远程对象网络所需的辅助组件。它们通常与构成特定网络的自定义注册副本类型配合使用。

例如,请看以下 .rep 文件:

class MyType {
    PROP(QString myProp="Hello World")
};

生成的副本可以注册为 QML 类型:

qmlRegisterType<MyTypeReplica>("custom",1,0,"MyTypeReplica")

随后可在 QML 中与基类 Node 配合使用:

import QtQuick
import QtRemoteObjects
import custom 1.0

Item {
    MyTypeReplica {
        id: myType
        node: Node { registryUrl: "local:registry" }
    }

    Text { text: myType.myProp }

    MouseArea {
        anchors.fill: parent
        onClicked: myType.pushMyProp("Updated Text")
    }
}

请注意,默认情况下无法直接对副本属性进行赋值,而需使用push 函数。这是因为声明式编程与异步更新混合使用可能会引发潜在问题。具体来说,我们需要避免如下问题:

myType.myProp = "Updated Text"
console.log(myType.myProp) // logs "Hello World", as the new text has not yet been round-tripped

可以通过在 .qml 文件中使用以下导入语句,将该模块中的 QML 类型导入到您的应用程序中:

import QtRemoteObjects

QML 类型

Host

Qt Remote Objects 网络上的主机节点

Node

Qt Remote Objects 网络上的节点

QtRemoteObjects

全局对象为在 QML 中处理远程类型提供了有用的函数

SettingsStore

用于持久化属性的基本存储

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.