本页内容

时间客户端应用程序

该客户端持有来自服务器的 Time 对象的副本,并在时钟中显示时间。

“时间客户端应用程序”会打开一个带有消息的蓝色窗口。单击一次,会显示一个时钟;再次单击,会显示两个时钟,随后循环显示。该时钟在 Clock.qml 中定义,并使用由 C++ 实现的 QML 类型 `Time `,通过服务器上的时间来设置时钟的时间。

显示两个同步模拟时钟的应用程序窗口

运行时间客户端应用程序前

要运行此示例,时间服务器应用程序必须已在与本应用程序相同的主机上运行。该应用程序会设置一个注册表服务器,并托管MinuteTimerSimpleSource 类的子类的实例。如果未运行,窗口中将显示错误消息。

使用 REP 文件定义远程对象

位于本应用程序父目录中的REP文件“timemodel.rep”用于生成这两个应用程序共用的头文件。对于本应用程序,生成的“rep_timemodel_replica.h”文件定义了MinuteTimerReplica 、MinuteTimer 的副本以及其他相关类。

TimeModel 类

plugin.h 和 plugin.cpp 中的TimeModel 类实现了 QML 类型Time 。它使用 URL “local:registry” 联系本地主机的注册服务器,获取一个副本MinuteTimerReplica ,并订阅其信号。属性会自动更新。

QML 类型

在“Clock.qml”中定义的 QML 通过 hours 和 minutes 属性绘制一只手表。

该应用程序会打开一个内容为“plugins.qml”的窗口。它通过 Loader 在不同内容之间切换,每次点击都会在“plugins0.qml”、“plugins1.qml”和“plugins2.qml”之间循环显示。

虽然“plugins0.qml”显示的是带有消息的蓝色屏幕,但“plugins1.qml”中使用了自定义的 QML 类型Clock ,其 hours 和 minutes 属性分别设置为Time 的时和分属性。“plugins2.qml”文件与此类似,但会显示两个时钟。

import QtRemoteObjects
import QtQuick
import TimeExample // import types from the plugin

Rectangle {
    width: 200
    height: 400
    color: "blue"
    Clock {
        id: clock1
        anchors.top: parent.top

        Time { // this class is defined in C++
            id: time
            node: node
        }

        Node {
            id: node
            registryUrl: "local:registry"
        }

        hours: time.hour
        minutes: time.minute
        valid: time.state == Time.Valid

    }
}

示例项目 @ 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.