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
qfuturewatcher.h
Go to the documentation of this file.
1// Copyright (C) 2020 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
5#ifndef QFUTUREWATCHER_H
6#define QFUTUREWATCHER_H
7
8#include <QtCore/qfuture.h>
9#include <QtCore/qobject.h>
10
12
13QT_BEGIN_NAMESPACE
14
15
16class QEvent;
17
18class QFutureWatcherBasePrivate;
19class Q_CORE_EXPORT QFutureWatcherBase : public QObject
20{
21 Q_OBJECT
22 Q_DECLARE_PRIVATE(QFutureWatcherBase)
23
24public:
25 explicit QFutureWatcherBase(QObject *parent = nullptr);
26 // de-inline dtor
27
28 int progressValue() const;
29 int progressMinimum() const;
30 int progressMaximum() const;
31 QString progressText() const;
32
33 bool isStarted() const;
34 bool isFinished() const;
35 bool isRunning() const;
36 bool isCanceled() const;
37#if QT_DEPRECATED_SINCE(6, 0)
38 QT_DEPRECATED_VERSION_X_6_0("Use isSuspending() or isSuspended() instead.")
39 bool isPaused() const;
40#endif
41 bool isSuspending() const;
42 bool isSuspended() const;
43
44 void waitForFinished();
45
46 void setPendingResultsLimit(int limit);
47
48 bool event(QEvent *event) override;
49
50Q_SIGNALS:
51 void started();
52 void finished();
53 void canceled();
54#if QT_DEPRECATED_SINCE(6, 0)
55 QT_DEPRECATED_VERSION_X_6_0("Use suspending() instead.")
56 void paused();
57#endif
58 void suspending();
59 void suspended();
60 void resumed();
61 void resultReadyAt(int resultIndex);
62 void resultsReadyAt(int beginIndex, int endIndex);
63 void progressRangeChanged(int minimum, int maximum);
64 void progressValueChanged(int progressValue);
65 void progressTextChanged(const QString &progressText);
66
67public Q_SLOTS:
68 void cancel();
69 void setSuspended(bool suspend);
70 void suspend();
71 void resume();
72 void toggleSuspended();
73
74#if QT_DEPRECATED_SINCE(6, 0)
75 QT_DEPRECATED_VERSION_X_6_0("Use setSuspended() instead.")
76 void setPaused(bool paused);
77
78 QT_DEPRECATED_VERSION_X_6_0("Use suspend() instead.")
79 void pause();
80
81 QT_DEPRECATED_VERSION_X_6_0("Use toggleSuspended() instead.")
82 void togglePaused();
83#endif
84
85protected:
86 void connectNotify (const QMetaMethod &signal) override;
87 void disconnectNotify (const QMetaMethod &signal) override;
88
89 // called from setFuture() implemented in template sub-classes
90 void connectOutputInterface();
91 void disconnectOutputInterface(bool pendingAssignment = false);
92
93private:
94 // implemented in the template sub-classes
95 virtual const QFutureInterfaceBase &futureInterface() const = 0;
96 virtual QFutureInterfaceBase &futureInterface() = 0;
97};
98
99template <typename T>
100class QFutureWatcher : public QFutureWatcherBase
101{
102public:
103 explicit QFutureWatcher(QObject *_parent = nullptr)
105 { }
107 { disconnectOutputInterface(); }
108
109 void setFuture(const QFuture<T> &future);
110 QFuture<T> future() const
111 { return m_future; }
112
113 template<typename U = T, typename = QtPrivate::EnableForNonVoid<U>>
114 T result() const { return m_future.result(); }
115
116 template<typename U = T, typename = QtPrivate::EnableForNonVoid<U>>
117 T resultAt(int index) const { return m_future.resultAt(index); }
118
119#ifdef Q_QDOC
120 int progressValue() const;
121 int progressMinimum() const;
122 int progressMaximum() const;
123 QString progressText() const;
124
125 bool isStarted() const;
126 bool isFinished() const;
127 bool isRunning() const;
128 bool isCanceled() const;
129#if QT_DEPRECATED_SINCE(6, 0)
130 bool isPaused() const;
131#endif
132 bool isSuspending() const;
133 bool isSuspended() const;
134
135 void waitForFinished();
136
137 void setPendingResultsLimit(int limit);
138
139Q_SIGNALS:
140 void started();
141 void finished();
142 void canceled();
143#if QT_DEPRECATED_SINCE(6, 0)
144 void paused();
145#endif
146 void suspending();
147 void suspended();
148 void resumed();
149 void resultReadyAt(int resultIndex);
150 void resultsReadyAt(int beginIndex, int endIndex);
151 void progressRangeChanged(int minimum, int maximum);
152 void progressValueChanged(int progressValue);
153 void progressTextChanged(const QString &progressText);
154
155public Q_SLOTS:
156 void cancel();
157 void setSuspended(bool suspend);
158 void suspend();
159 void resume();
160 void toggleSuspended();
161#if QT_DEPRECATED_SINCE(6, 0)
162 void setPaused(bool paused);
163 void pause();
164 void togglePaused();
165#endif // QT_DEPRECATED_SINCE(6, 0)
166
167#endif // Q_QDOC
168
169private:
170 QFuture<T> m_future;
171 const QFutureInterfaceBase &futureInterface() const override { return m_future.d; }
172 QFutureInterfaceBase &futureInterface() override { return m_future.d; }
173};
174
175template <typename T>
176Q_INLINE_TEMPLATE void QFutureWatcher<T>::setFuture(const QFuture<T> &_future)
177{
178 if (_future.d == m_future.d)
179 return;
180
181 disconnectOutputInterface(true);
182 m_future = _future;
183 connectOutputInterface();
184}
185
186QT_END_NAMESPACE
187
188#endif // QFUTUREWATCHER_H
void setFuture(const QFuture< T > &future)
Starts watching the given future.
const QFutureInterfaceBase & futureInterface() const override
QFutureInterfaceBase & futureInterface() override
T resultAt(int index) const
QFuture< T > future() const
Returns the watched future.
QFutureWatcher(QObject *_parent=nullptr)
Constructs a new QFutureWatcher with the given parent.
~QFutureWatcher()
Destroys the QFutureWatcher.
QFuture< void > future
[5]