模型-视图-客户端

开发一个非常简单的客户端程序,用于显示服务器上的内容及其变更。

这是与模型-视图服务器配套的客户端应用程序。

    QRemoteObjectNode node(QUrl(QStringLiteral("local:registry")));
    node.setHeartbeatInterval(1000);

首先,我们创建一个QRemoteObjectNode ,并将其连接到本地机器上的一个注册表。我们还设置了一个heartbeat interval 。心跳功能有助于检测与源的连接是否中断。在这种情况下,由于所有流量都在本地,因此当服务器关闭时,它会检测到。

    QScopedPointer<QAbstractItemModelReplica> model(node.acquireModel(QStringLiteral("RemoteModel")));

接着,我们通过acquire 获取包含所有数据的模型。在此示例中,我们试图从已连接的远程对象网络中获取名为RemoteModel 的模型。

    QTreeView view;
    view.setWindowTitle(QStringLiteral("RemoteView"));
    view.resize(640,480);
    view.setModel(model.data());
    view.show();

最后,我们在一个非常基础的应用程序中显示该模型。

示例项目 @ code.qt.io

© 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.