| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2017 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 |
** Contact: http://www.qt-project.org/legal |
| 5 |
** |
| 6 |
** This file is part of the documentation of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:FDL$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU Free Documentation License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU Free |
| 19 |
** Documentation License version 1.3 as published by the Free Software |
| 20 |
** Foundation and appearing in the file included in the packaging of |
| 21 |
** this file. Please review the following information to ensure |
| 22 |
** the GNU Free Documentation License version 1.3 requirements |
| 23 |
** will be met: https://www.gnu.org/licenses/fdl-1.3.html. |
| 24 |
** $QT_END_LICENSE$ |
| 25 |
** |
| 26 |
****************************************************************************/ |
| 27 |
|
| 28 |
/*! |
| 29 |
\example battery-charge |
| 30 |
\ingroup publishsubscribe-examples |
| 31 |
\title Accessing Publish and Subscribe from QML |
| 32 |
|
| 33 |
This examples shows how to access published context values from within QML. The example |
| 34 |
consists of two programs. The first, battery-publisher, is a standard Qt Widgets application |
| 35 |
to emulate a battery for the sole purpose of demonstrating the second program. It publishes |
| 36 |
the keys |
| 37 |
|
| 38 |
\code |
| 39 |
/power/battery/charge |
| 40 |
/power/battery/charging |
| 41 |
\endcode |
| 42 |
|
| 43 |
and provides controls for modifying their values. |
| 44 |
|
| 45 |
\image battery-publisher.png |
| 46 |
|
| 47 |
The second program, battery-subscriber, is implemented in pure QML. It has the following |
| 48 |
features: A rectangular area representing the percent charge of the battery. It indicates a |
| 49 |
low battery state by changing the color to red, it is green otherwise. An animation is shown |
| 50 |
to indicate that the battery is being recharged. |
| 51 |
|
| 52 |
\image battery-subscriber.png |
| 53 |
|
| 54 |
\section2 Battery Subscriber |
| 55 |
|
| 56 |
First, import the plugin library that provides the ValueSpaceSubscriber QML element. |
| 57 |
|
| 58 |
\snippet battery-charge/battery-subscriber/battery-subscriber.qml 4 |
| 59 |
|
| 60 |
Two ValueSpaceSubscriber instances are created, one for each of the battery values. We give |
| 61 |
each object a unique id so that we can reference it from elsewhere in the QML. We set the |
| 62 |
\e path properties to the Value Space path of the keys. |
| 63 |
|
| 64 |
\snippet battery-charge/battery-subscriber/battery-subscriber.qml 0 |
| 65 |
|
| 66 |
The default state of the rectangle used to visualize the battery charge uses the \e charge |
| 67 |
property of our BatteryCharge class in the expression for its height. |
| 68 |
|
| 69 |
\snippet battery-charge/battery-subscriber/battery-subscriber.qml 1 |
| 70 |
|
| 71 |
When the battery charge changes the height of the rectangle will automatically change. |
| 72 |
|
| 73 |
Next we define two additional states. The \e low state is entered when the battery charge |
| 74 |
drops below 25% and the battery is not being recharged. When in this state the color is set to |
| 75 |
red. |
| 76 |
|
| 77 |
\snippet battery-charge/battery-subscriber/battery-subscriber.qml 2 |
| 78 |
|
| 79 |
The \e charging state is entered when the battery is being recharged. When in this state a |
| 80 |
particle effect animation is enabled. |
| 81 |
|
| 82 |
\snippet battery-charge/battery-subscriber/battery-subscriber.qml 3 |
| 83 |
*/ |