Qt Quick Controls - 可穿戴演示

Demonstrates an application launcher designed for wearable devices.

The 可穿戴演示 consists of an application launcher and a collection of small and simple example applications aimed at wearable devices.

Structure

The main .qml file, wearable.qml , consists of an ApplicationWindow StackView for a stack-based navigation model, and buttons for interactive navigation.

QQC2.ApplicationWindow {
    id: window
    ...
    header: NaviButton {
        id: homeButton
        ...
        onClicked: stackView.pop(null)
    }
    footer: NaviButton {
        id: backButton
        ...
        onClicked: stackView.pop()
    }
    QQC2.StackView {
        id: stackView
        ...
            onLaunched: stackView.push(page)
        }
    }
    DemoMode {
        stackView: stackView
    }
    DemoModeIndicator {
        id: demoModeIndicator
        y: settings.demoMode ? -height : -height * 2
        anchors.horizontalCenter: parent.horizontalCenter
        height: header.height
        z: window.header.z + 1
    }
    MouseArea {
        enabled: settings.demoMode
        anchors.fill: parent
        onClicked: {
            // Stop demo mode and return to the launcher page.
            settings.demoMode = false
            stackView.pop(null)
        }
    }
}
					
					

Styling

The demo uses a custom Qt Quick Controls 2 style embedded into the demo's resources. The custom style is implemented for a few controls only, as it is specific to this particular demo. It uses a singleton type for various styling attributes, such as fonts and colors.

  • qml/Style/PageIndicator.qml
  • qml/Style/Slider.qml
  • qml/Style/Switch.qml
  • qml/Style/UIStyle.qml

The style is applied in main() in wearable.cpp :

QQuickStyle::setStyle(QStringLiteral("qrc:/qml/Style"));
					

The main benefit of using the built-in styling system is that the style selection is fully transparent to the application code. There is no need to import a specific folder that contains the styled controls. This way, the application can be run with other styles too.

自定义类型

The demo application contains a custom button type implemented in qml/NaviButton.qml . The navigation button is used as a home and back button in wearable.qml . NaviButton extends the AbstractButton type with properties that control the slide in and slide out transitions and the button image.

QQC2.AbstractButton {
    id: button
    property int edge: Qt.TopEdge
    property alias imageSource: image.source
    contentItem: Image {
        id: image
        ...
    }
    background: Rectangle {
        ...
    }
    transform: Translate {
        Behavior on y { NumberAnimation { } }
        y: enabled ? 0 : edge === Qt.TopEdge ? -button.height : button.height
    }
}
					
					

图标

The demo ships a custom icon theme. The icons are bundled into the :/icons folder in the application's resources. The index.theme file lists the contents of the icon theme:

[Icon Theme]
Name=Wearable
Comment=Qt Quick Controls 2 Wearable Demo Icon Theme
Directories=36x36,36x36@2
[36x36]
Size=36
Type=Fixed
[36x36@2]
Size=36
Scale=2
Type=Fixed
					

Finally, the icon theme is selected in main() :

QIcon::setThemeName(QStringLiteral("wearable"));
					

The icons are used on the Launcher Page, which is presented below. See Icons in Qt Quick Controls for more details about icons and themes.

Launcher Page

The application launcher is implemented using a circular PathView in LauncherPage.qml . Each application is in a separate .qml file, which is added to the ListModel on the launcher page.

PathView {
    id: circularView
    signal launched(string page)
    ...
    model: ListModel {
        ListElement {
            title: qsTr("World Clock")
            icon: "worldclock"
            page: "WorldClock/WorldClockPage.qml"
        }
        ...
        ListElement {
            title: qsTr("Settings")
            icon: "settings"
            page: "Settings/SettingsPage.qml"
        }
    }
    delegate: QQC2.RoundButton {
        ...
        icon.width: 36
        icon.height: 36
        icon.name: model.icon
        ...
        onClicked: {
            if (PathView.isCurrentItem)
                circularView.launched(Qt.resolvedUrl(page))
            else
                circularView.currentIndex = index
        }
    }
    ...
}
					
					

应用程序

The applications are designed for touch input based on what input methods or communication means are typically offered by wearable devices.

Most applications have their own JavaScript files that act as dummy application backends. They illustrate fetching external data and help manipulating or converting the data. In the 导航 and Weather applications, data acquisition is implemented using XMLHttpRequest to read from local files. These files were generated by storing responses from remote servers in JSON format. This code can be easily modified to acquire data from remote servers.

This application displays a walking route from source to destination. This route information is obtained using the REST API provided by https://www.openstreetmap.org/ . The API response is in JSON format, which is parsed using JavaScript by the application. Currently, it is not possible to specify the source and destination from within the application, but it can be added based on the device's capabilities. For example, user input could be implemented as screens with input fields, or can be communicated over Bluetooth/Wifi from a paired application running on another device such as a smart phone or PC.

Weather

This application displays weather information such as temperature, sunrise and sunset times, air pressure, and so on. This information is obtained from https://openweathermap.org/ using its REST API. The API response is in JSON format, which is parsed using JavaScript by the application. This application can also be modified by adding screens to obtain weather data for a given location.

World Clock

This application displays a world clock for different cities. As of now, the list of cities is hard-coded in the application, but that can be changed based on the input capabilities of the device.

Others

The remaining applications return static data for now, but they can be modified to process response data obtained from respective services.

运行范例

要运行范例从 Qt Creator ,打开 欢迎 模式,然后选择范例从 范例 。更多信息,拜访 构建和运行范例 .

范例工程 @ code.qt.io