Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickprogressbar.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
7
9
10/*!
11 \qmltype ProgressBar
12 \inherits Control
13//! \nativetype QQuickProgressBar
14 \inqmlmodule QtQuick.Controls
15 \since 5.7
16 \ingroup qtquickcontrols-indicators
17 \brief Indicates the progress of an operation.
18
19 \image qtquickcontrols-progressbar.gif
20
21 ProgressBar indicates the progress of an operation. The value should be updated
22 regularly. The range is defined by \l from and \l to, which both can contain any value.
23
24 \code
25 ProgressBar {
26 value: 0.5
27 }
28 \endcode
29
30 ProgressBar also supports a special \l indeterminate mode, which is useful,
31 for example, when unable to determine the size of the item being downloaded,
32 or if the download progress gets interrupted due to a network disconnection.
33
34 \image qtquickcontrols-progressbar-indeterminate.gif
35
36 \code
37 ProgressBar {
38 indeterminate: true
39 }
40 \endcode
41
42 The indeterminate mode is similar to a \l BusyIndicator. Both can be used
43 to indicate background activity. The main difference is visual, and that
44 ProgressBar can also present a concrete amount of progress (when it can be
45 determined). Due to the visual difference, indeterminate progress bars and
46 busy indicators fit different places in user interfaces. Typical places for
47 an indeterminate progress bar:
48 \list
49 \li at the bottom of a \l ToolBar
50 \li inline within the content of a \l Page
51 \li in an \l ItemDelegate to show the progress of a particular item
52 \endlist
53
54 \sa {Customizing ProgressBar}, BusyIndicator, {Indicator Controls}
55*/
56
58{
59public:
61 qreal to = 1;
63 bool indeterminate = false;
64};
65
66QQuickProgressBar::QQuickProgressBar(QQuickItem *parent)
67 : QQuickControl(*(new QQuickProgressBarPrivate), parent)
68{
69 Q_D(QQuickProgressBar);
70 d->setSizePolicy(QLayoutPolicy::Expanding, QLayoutPolicy::Fixed);
71}
72
73/*!
74 \qmlproperty real QtQuick.Controls::ProgressBar::from
75
76 This property holds the starting value for the progress. The default value is \c 0.0.
77
78 \sa to, value
79*/
80qreal QQuickProgressBar::from() const
81{
82 Q_D(const QQuickProgressBar);
83 return d->from;
84}
85
86void QQuickProgressBar::setFrom(qreal from)
87{
88 Q_D(QQuickProgressBar);
89 if (qFuzzyCompare(d->from, from))
90 return;
91
92 d->from = from;
93 emit fromChanged();
94 emit positionChanged();
95 emit visualPositionChanged();
96 if (isComponentComplete())
97 setValue(d->value);
98}
99
100/*!
101 \qmlproperty real QtQuick.Controls::ProgressBar::to
102
103 This property holds the end value for the progress. The default value is \c 1.0.
104
105 \sa from, value
106*/
107qreal QQuickProgressBar::to() const
108{
109 Q_D(const QQuickProgressBar);
110 return d->to;
111}
112
113void QQuickProgressBar::setTo(qreal to)
114{
115 Q_D(QQuickProgressBar);
116 if (qFuzzyCompare(d->to, to))
117 return;
118
119 d->to = to;
120 emit toChanged();
121 emit positionChanged();
122 emit visualPositionChanged();
123 if (isComponentComplete())
124 setValue(d->value);
125}
126
127/*!
128 \qmlproperty real QtQuick.Controls::ProgressBar::value
129
130 This property holds the progress value. The default value is \c 0.0.
131
132 \sa from, to, position
133*/
134qreal QQuickProgressBar::value() const
135{
136 Q_D(const QQuickProgressBar);
137 return d->value;
138}
139
140void QQuickProgressBar::setValue(qreal value)
141{
142 Q_D(QQuickProgressBar);
143 if (isComponentComplete())
144 value = d->from > d->to ? qBound(d->to, value, d->from) : qBound(d->from, value, d->to);
145
146 if (qFuzzyCompare(d->value, value))
147 return;
148
149 d->value = value;
150 emit valueChanged();
151 emit positionChanged();
152 emit visualPositionChanged();
153}
154
155/*!
156 \qmlproperty real QtQuick.Controls::ProgressBar::position
157 \readonly
158
159 This property holds the logical position of the progress.
160
161 The position is expressed as a fraction of the value, in the range
162 \c {0.0 - 1.0}. For visualizing the progress, the right-to-left
163 aware \l visualPosition should be used instead.
164
165 \sa value, visualPosition
166*/
167qreal QQuickProgressBar::position() const
168{
169 Q_D(const QQuickProgressBar);
170 if (qFuzzyCompare(d->from, d->to))
171 return 0;
172 return (d->value - d->from) / (d->to - d->from);
173}
174
175/*!
176 \qmlproperty real QtQuick.Controls::ProgressBar::visualPosition
177 \readonly
178
179 This property holds the visual position of the progress.
180
181 The position is expressed as a fraction of the value, in the range \c {0.0 - 1.0}.
182 When the control is \l {Control::mirrored}{mirrored}, \c visuaPosition is equal
183 to \c {1.0 - position}. This makes \c visualPosition suitable for visualizing
184 the progress, taking right-to-left support into account.
185
186 \sa position, value
187*/
188qreal QQuickProgressBar::visualPosition() const
189{
190 if (isMirrored())
191 return 1.0 - position();
192 return position();
193}
194
195/*!
196 \qmlproperty bool QtQuick.Controls::ProgressBar::indeterminate
197
198 This property holds whether the progress bar is in indeterminate mode.
199 A progress bar in indeterminate mode displays that an operation is in progress, but it
200 doesn't show how much progress has been made.
201
202 \image qtquickcontrols-progressbar-indeterminate.gif
203*/
204bool QQuickProgressBar::isIndeterminate() const
205{
206 Q_D(const QQuickProgressBar);
207 return d->indeterminate;
208}
209
210void QQuickProgressBar::setIndeterminate(bool indeterminate)
211{
212 Q_D(QQuickProgressBar);
213 if (d->indeterminate == indeterminate)
214 return;
215
216 d->indeterminate = indeterminate;
217 emit indeterminateChanged();
218}
219
220void QQuickProgressBar::mirrorChange()
221{
222 QQuickControl::mirrorChange();
223 if (!qFuzzyCompare(position(), qreal(0.5)))
224 emit visualPositionChanged();
225}
226
227void QQuickProgressBar::componentComplete()
228{
229 Q_D(QQuickProgressBar);
230 QQuickControl::componentComplete();
231 setValue(d->value);
232}
233
234#if QT_CONFIG(accessibility)
235QAccessible::Role QQuickProgressBar::accessibleRole() const
236{
237 return QAccessible::ProgressBar;
238}
239#endif
240
241QT_END_NAMESPACE
242
243#include "moc_qquickprogressbar_p.cpp"
Indicates the progress of an operation.