时间客户端应用程序

时间客户端应用程序(Time Client Application)是一种客户端程序,它从服务器上获取时间对象的副本,并在时钟中显示时间。

时间客户端应用程序会打开一个蓝色窗口,并显示一条信息。点击一次,就会显示一个时钟。再次点击时,会显示两个时钟,然后重复显示。时钟在 Clock.qml 中定义,并使用Time QML-type(以 C++ 实现),使用服务器上的时间设置时钟的时间。

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

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

使用 REP 文件定义远程对象

本应用程序父目录中的 REP 文件 "timemodel.rep "用于生成两个应用程序使用的头文件。在本应用程序中,生成的 "rep_timemodel_replica.h "文件定义了MinuteTimerReplicaMinuteTimer 的副本以及其他相关类。

TimeModel 类

plugin.h 和 plugin.cpp 中的TimeModel 类实现了 QML 类型的Time 。它使用 URL "local:registry"(本地:注册表)联系 localhost 上的注册表服务器,获取副本MinuteTimerReplica ,并连接到其信号。属性会自动更新。

QML 类型

Clock.qml "中定义的 QML 使用小时和分钟属性绘制手表。

应用程序会打开一个内容为 "plugins.qml "的窗口。它使用加载器在显示不同内容之间切换,每次点击都在 "plugins0.qml"、"plugins1.qml "和 "plugins2.qml "之间循环。

plugins0.qml "显示的是带信息的蓝屏,而 "plugins1.qml "使用的是自定义 QML 类型的Clock ,其小时和分钟属性设置为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

另请参阅 时间服务器应用程序

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