Qt 5.11 | Qt Quick Controls 2 | Qt Quick Controls 2 Examples | Qt Quick Controls 2 - Wearable Demo

Qt 5.11.3 Reference Documentation

# Qt Quick Controls 2 - Wearable Demo

Demonstrates an application launcher designed for wearable devices.

The Wearable Demo 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, a 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)
              }
          }
      }

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

## Custom Type

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
          }
      }

## Icons

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 2 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
              }
          }
          ...
      }

## Applications

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 `Navigation` 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.

### Navigation

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.

## Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Files:

- wearable/qml/Alarms/AlarmsPage.qml
- wearable/qml/Fitness/FitnessPage.qml
- wearable/qml/Fitness/fitness.js
- wearable/qml/LauncherPage.qml
- wearable/qml/NaviButton.qml
- wearable/qml/Navigation/NavigationPage.qml
- wearable/qml/Navigation/RouteElement.qml
- wearable/qml/Navigation/navigation.js
- wearable/qml/Notifications/NotificationsPage.qml
- wearable/qml/Notifications/notifications.js
- wearable/qml/Settings/SettingsPage.qml
- wearable/qml/Style/PageIndicator.qml
- wearable/qml/Style/Slider.qml
- wearable/qml/Style/Switch.qml
- wearable/qml/Style/UIStyle.qml
- wearable/qml/Style/qmldir
- wearable/qml/SwipeViewPage.qml
- wearable/qml/Weather/WeatherPage.qml
- wearable/qml/Weather/weather.js
- wearable/qml/WorldClock/Clock.qml
- wearable/qml/WorldClock/WorldClockPage.qml
- wearable/wearable.cpp
- wearable/wearable.pro
- wearable/wearable.qml
- wearable/wearable.qrc

Images:

- wearable/icons/wearable/36x36/alarms.png
- wearable/icons/wearable/36x36/fitness.png
- wearable/icons/wearable/36x36/navigation.png
- wearable/icons/wearable/36x36/notifications.png
- wearable/icons/wearable/36x36/settings.png
- wearable/icons/wearable/36x36/weather.png
- wearable/icons/wearable/36x36/worldclock.png
- wearable/icons/wearable/36x36@2/alarms.png
- wearable/icons/wearable/36x36@2/fitness.png
- wearable/icons/wearable/36x36@2/navigation.png
- wearable/icons/wearable/36x36@2/notifications.png
- wearable/icons/wearable/36x36@2/settings.png
- wearable/icons/wearable/36x36@2/weather.png
- wearable/icons/wearable/36x36@2/worldclock.png
- wearable/images/back.png
- wearable/images/back@2x.png
- wearable/images/back@3x.png
- wearable/images/back@4x.png
- wearable/images/background.png
- wearable/images/home.png
- wearable/images/home@2x.png
- wearable/images/home@3x.png
- wearable/images/home@4x.png
- wearable/qml/Fitness/images/man-running.png
- wearable/qml/Fitness/images/man-running@2x.png
- wearable/qml/Fitness/images/man-walking.png
- wearable/qml/Fitness/images/man-walking@2x.png
- wearable/qml/Navigation/images/end.png
- wearable/qml/Navigation/images/end@2x.png
- wearable/qml/Navigation/images/leftturn.png
- wearable/qml/Navigation/images/leftturn@2x.png
- wearable/qml/Navigation/images/marker.png
- wearable/qml/Navigation/images/navigation.png
- wearable/qml/Navigation/images/navigation@2x.png
- wearable/qml/Navigation/images/rightturn.png
- wearable/qml/Navigation/images/rightturn@2x.png
- wearable/qml/Navigation/images/start.png
- wearable/qml/Navigation/images/start@2x.png
- wearable/qml/Navigation/images/straight.png
- wearable/qml/Navigation/images/straight@2x.png
- wearable/qml/Navigation/images/uturn.png
- wearable/qml/Navigation/images/uturn@2x.png
- wearable/qml/Notifications/images/avatarf.png
- wearable/qml/Notifications/images/avatarf@2x.png
- wearable/qml/Notifications/images/avatarm.png
- wearable/qml/Notifications/images/avatarm@2x.png
- wearable/qml/Notifications/images/missedcall.png
- wearable/qml/Notifications/images/missedcall@2x.png
- wearable/qml/Settings/images/bluetooth.png
- wearable/qml/Settings/images/bluetooth@2x.png
- wearable/qml/Settings/images/brightness.png
- wearable/qml/Settings/images/brightness@2x.png
- wearable/qml/Settings/images/contrast.png
- wearable/qml/Settings/images/contrast@2x.png
- wearable/qml/Settings/images/wifi.png
- wearable/qml/Settings/images/wifi@2x.png
- wearable/qml/Weather/images/humidity.png
- wearable/qml/Weather/images/humidity@2x.png
- wearable/qml/Weather/images/pressure.png
- wearable/qml/Weather/images/pressure@2x.png
- wearable/qml/Weather/images/sunrise.png
- wearable/qml/Weather/images/sunrise@2x.png
- wearable/qml/Weather/images/sunset.png
- wearable/qml/Weather/images/sunset@2x.png
- wearable/qml/Weather/images/temperature.png
- wearable/qml/Weather/images/temperature@2x.png
- wearable/qml/Weather/images/wind.png
- wearable/qml/Weather/images/wind@2x.png
- wearable/qml/WorldClock/images/center.png
- wearable/qml/WorldClock/images/center@2x.png
- wearable/qml/WorldClock/images/clock-night.png
- wearable/qml/WorldClock/images/clock-night@2x.png
- wearable/qml/WorldClock/images/clock.png
- wearable/qml/WorldClock/images/second.png
- wearable/qml/WorldClock/images/second@2x.png
- wearable/qml/WorldClock/images/swissdaydial.png
- wearable/qml/WorldClock/images/swissdaydial@2x.png
- wearable/qml/WorldClock/images/swissdayhour.png
- wearable/qml/WorldClock/images/swissdayhour@2x.png
- wearable/qml/WorldClock/images/swissdayminute.png
- wearable/qml/WorldClock/images/swissdayminute@2x.png
- wearable/qml/WorldClock/images/swissnightdial.png
- wearable/qml/WorldClock/images/swissnightdial@2x.png
- wearable/qml/WorldClock/images/swissnighthour.png
- wearable/qml/WorldClock/images/swissnighthour@2x.png
- wearable/qml/WorldClock/images/swissnightminute.png
- wearable/qml/WorldClock/images/swissnightminute@2x.png
- wearable/qml/WorldClock/images/swissseconds.png