Qt Quick Controls - 可穿戴设备演示
演示专为可穿戴设备设计的应用程序启动器。
可穿戴演示包括一个应用程序启动器和一系列针对可穿戴设备的小型、简单示例应用程序。
结构
主 .qml 文件wearable.qml
由ApplicationWindow 、用于基于堆栈的导航模型的StackView 和用于交互式导航的按钮组成。
QQC2.ApplicationWindow { id: window ... QQC2.StackView { id: stackView ... initialItem: LauncherPage { onLaunched: (title, page, fallback) => { var createdPage = Qt.createComponent(page) if (createdPage.status !== Component.Ready) createdPage = Qt.createComponent(fallback) stackView.push(createdPage) header.title = title } } ... } DemoMode { stackView: stackView } DemoModeIndicator { id: demoModeIndicator y: WearableSettings.demoMode ? header.height + 3 : -height - 5 anchors.horizontalCenter: parent.horizontalCenter z: header.z + 1 } MouseArea { enabled: WearableSettings.demoMode anchors.fill: parent onClicked: { // Stop demo mode and return to the launcher page. WearableSettings.demoMode = false stackView.pop(null) } } }
样式
该演示使用嵌入到演示资源中的自定义Qt Quick Controls 2 风格。自定义样式只针对几个控件实施,因为它是本演示的专用样式。它为各种样式属性(如字体和颜色)使用了单例类型。
WearableStyle/PageIndicator.qml
WearableStyle/Slider.qml
WearableStyle/Switch.qml
WearableStyle/UIStyle.qml
该样式在main()
wearable.cpp
中应用:
QQuickStyle::setStyle(QStringLiteral("WearableStyle"));
使用内置样式系统的主要好处是,样式选择对应用程序代码完全透明。无需导入包含样式控件的特定文件夹。这样,应用程序也可以使用其他样式运行。
启动器页面
应用程序启动器是通过LauncherPage.qml
中的循环PathView 实现的。每个应用程序都有一个单独的 .qml 文件,这些文件被添加到启动器页面上的ListModel 中。对于某些应用程序,还提供了一个回退选项,以处理 QtLocation 等可选依赖项。
PathView { id: circularView signal launched(string title, string page, string fallbackpage) ... model: ListModel { ListElement { title: qsTr("Navigation") pageIcon: "maps" page: "NavigationPage.qml" fallback: "NavigationFallbackPage.qml" } ... ListElement { title: qsTr("Settings") pageIcon: "settings" page: "SettingsPage.qml" fallback: "" } } delegate: QQC2.RoundButton { ... icon.width: 36 icon.height: 36 icon.source: UIStyle.iconPath(pageIcon) icon.color: UIStyle.textColor ... onClicked: { if (PathView.isCurrentItem) circularView.launched(title, Qt.resolvedUrl(page), Qt.resolvedUrl(fallback)) else circularView.currentIndex = index } } ... }
应用程序
这些应用程序是根据可穿戴设备通常提供的输入方法或通信手段为触摸输入而设计的。
大多数应用程序都有自己的 JavaScript 文件,作为虚拟应用程序的后端。它们演示了如何获取、操作或转换外部数据。例如,Weather
应用程序使用XMLHttpRequest 从本地文件读取数据。这些文件是通过以 JSON 格式存储远程服务器的响应生成的。这段代码可以轻松修改,以便从远程服务器获取数据。
导航
该应用程序使用 QtLocation 模块显示奥斯陆的路线。如果未安装 QtLocation 模块,则会以静态图像显示地图和基于 JSON 文件的路线信息。目前,还无法在应用程序中指定来源地和目的地,但可以根据设备的功能进行添加。例如,您可以使用以下方法之一收集必要的信息:
- 执行附加屏幕以收集用户输入
- 通过蓝牙或 WiFi 频道与其他设备(智能手机或 PC)通信。
天气
本应用程序显示天气信息,如温度、日出日落时间、气压等。这些信息通过https://openweathermap.org/的 REST API 获取。API 响应为 JSON 格式,应用程序使用 JavaScript 对其进行解析。还可以通过添加屏幕来修改此应用程序,以获取指定地点的天气数据。
世界时钟
此应用程序可显示不同城市的世界时钟。目前,城市列表是硬编码在应用程序中的,但可以根据设备的输入能力进行更改。
其他应用程序
其余应用程序暂时返回静态数据,但可以对其进行修改,以处理从相关服务获取的响应数据。
运行示例
要从 Qt Creator,打开Welcome 模式,然后从Examples 中选择示例。更多信息,请参阅Qt Creator: 教程:构建并运行。
© 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.