QFutureWatcher Class▲
-
Header: QFutureWatcher
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Inherits: QObject
-
Group: QFutureWatcher is part of thread
Detailed Description▲
QFutureWatcher provides information and notifications about a QFuture. Use the setFuture() function to start watching a particular QFuture. The future() function returns the future set with setFuture().
For convenience, several of QFuture's functions are also available in QFutureWatcher: progressValue(), progressMinimum(), progressMaximum(), progressText(), isStarted(), isFinished(), isRunning(), isCanceled(), isSuspending(), isSuspended(), waitForFinished(), result(), and resultAt(). The cancel(), setSuspended(), suspend(), resume(), and toggleSuspended() functions are slots in QFutureWatcher.
Status changes are reported via the started(), finished(), canceled(), suspending(), suspended(), resumed(), resultReadyAt(), and resultsReadyAt() signals. Progress information is provided from the progressRangeChanged(), void progressValueChanged(), and progressTextChanged() signals.
Throttling control is provided by the setPendingResultsLimit() function. When the number of pending resultReadyAt() or resultsReadyAt() signals exceeds the limit, the computation represented by the future will be throttled automatically. The computation will resume once the number of pending signals drops below the limit.
Example: Starting a computation and getting a slot callback when it's finished:
// Instantiate the objects and connect to the finished signal.
MyClass myObject;
QFutureWatcher&
lt;int
&
gt; watcher;
connect(&
amp;watcher, &
amp;QFutureWatcher&
lt;int
&
gt;::
finished, &
amp;myObject, &
amp;MyClass::
handleFinished);
// Start the computation.
QFuture&
lt;int
&
gt; future =
QtConcurrent::
run(...);
watcher.setFuture(future);
Be aware that not all running asynchronous computations can be canceled or suspended. For example, the future returned by QtConcurrent::run() cannot be canceled; but the future returned by QtConcurrent::mappedReduced() can.
QFutureWatcher<void> is specialized to not contain any of the result fetching functions. Any QFuture<T> can be watched by a QFutureWatcher<void> as well. This is useful if only status or progress information is needed; not the actual result data.
See Also▲
See also QFuture, Qt Concurrent
Member Function Documentation▲
[explicit] QFutureWatcher::QFutureWatcher(QObject *parent = nullptr)▲
Constructs a new QFutureWatcher with the given parent. Until a future is set with setFuture(), the functions isStarted(), isCanceled(), and isFinished() return true.
[virtual] QFutureWatcher::~QFutureWatcher()▲
Destroys the QFutureWatcher.
void QFutureWatcher::cancel()▲
Cancels the asynchronous computation represented by the future(). Note that the cancellation is asynchronous. Use waitForFinished() after calling cancel() when you need synchronous cancellation.
Currently available results may still be accessed on a canceled QFuture, but new results will not become available after calling this function. Also, this QFutureWatcher will not deliver progress and result ready signals once canceled. This includes the progressValueChanged(), progressRangeChanged(), progressTextChanged(), resultReadyAt(), and resultsReadyAt() signals.
Be aware that not all running asynchronous computations can be canceled. For example, the QFuture returned by QtConcurrent::run() cannot be canceled; but the QFuture returned by QtConcurrent::mappedReduced() can.
void QFutureWatcher::canceled()▲
This signal is emitted if the watched future is canceled.
void QFutureWatcher::finished()▲
This signal is emitted when the watched future finishes.
QFuture<T> QFutureWatcher::future() const▲
bool QFutureWatcher::isCanceled() const▲
Returns true if the asynchronous computation has been canceled with the cancel() function, or if no future has been set; otherwise returns false.
Be aware that the computation may still be running even though this function returns true. See cancel() for more details.
bool QFutureWatcher::isFinished() const▲
Returns true if the asynchronous computation represented by the future() has finished, or if no future has been set; otherwise returns false.
bool QFutureWatcher::isRunning() const▲
Returns true if the asynchronous computation represented by the future() is currently running; otherwise returns false.
bool QFutureWatcher::isStarted() const▲
Returns true if the asynchronous computation represented by the future() has been started, or if no future has been set; otherwise returns false.
[since 6.0] bool QFutureWatcher::isSuspended() const▲
Returns true if a suspension of the asynchronous computation has been requested, and it is in effect, meaning that no more results or progress changes are expected.
This function was introduced in Qt 6.0.
See Also▲
See also suspended(), setSuspended(), isSuspending()
[since 6.0] bool QFutureWatcher::isSuspending() const▲
Returns true if the asynchronous computation has been suspended with the suspend() function, but the work is not yet suspended, and computation is still running. Returns false otherwise.
To check if suspension is actually in effect, use isSuspended() instead.
This function was introduced in Qt 6.0.
See Also▲
See also setSuspended(), toggleSuspended(), isSuspended()
int QFutureWatcher::progressMaximum() const▲
int QFutureWatcher::progressMinimum() const▲
void QFutureWatcher::progressRangeChanged(int minimum, int maximum)▲
The progress range for the watched future has changed to minimum and maximum
QString QFutureWatcher::progressText() const▲
Returns the (optional) textual representation of the progress as reported by the asynchronous computation.
Be aware that not all computations provide a textual representation of the progress, and as such, this function may return an empty string.
void QFutureWatcher::progressTextChanged(const QString &progressText)▲
This signal is emitted when the watched future reports textual progress information, progressText.
int QFutureWatcher::progressValue() const▲
Returns the current progress value, which is between the progressMinimum() and progressMaximum().
See Also▲
See also progressMinimum(), progressMaximum()
void QFutureWatcher::progressValueChanged(int progressValue)▲
This signal is emitted when the watched future reports progress, progressValue gives the current progress. In order to avoid overloading the GUI event loop, QFutureWatcher limits the progress signal emission rate. This means that listeners connected to this slot might not get all progress reports the future makes. The last progress update (where progressValue equals the maximum value) will always be delivered.
T QFutureWatcher::result() const▲
Returns the first result in the future(). If the result is not immediately available, this function will block and wait for the result to become available. This is a convenience method for calling resultAt(0).
See Also▲
See also resultAt()
T QFutureWatcher::resultAt(int index) const▲
Returns the result at index in the future(). If the result is not immediately available, this function will block and wait for the result to become available.
See Also▲
See also result()
void QFutureWatcher::resultReadyAt(int index)▲
This signal is emitted when the watched future reports a ready result at index. If the future reports multiple results, the index will indicate which one it is. Results can be reported out-of-order. To get the result, call resultAt(index);
void QFutureWatcher::resultsReadyAt(int beginIndex, int endIndex)▲
This signal is emitted when the watched future reports ready results. The results are indexed from beginIndex to endIndex.
void QFutureWatcher::resume()▲
Resumes the asynchronous computation represented by the future(). This is a convenience method that simply calls setSuspended(false).
See Also▲
See also suspend()
void QFutureWatcher::resumed()▲
This signal is emitted when the watched future is resumed.
void QFutureWatcher::setFuture(const QFuture<T> &future)▲
Starts watching the given future.
If future has already started, the watcher will initially emit signals that bring their listeners up to date about the future's state. The following signals will, if applicable, be emitted in the given order: started(), progressRangeChanged(), progressValueChanged(), progressTextChanged(), resultsReadyAt(), resultReadyAt(), suspending(), suspended(), canceled(), and finished(). Of these, resultsReadyAt() and resultReadyAt() may be emitted several times to cover all available results. progressValueChanged() and progressTextChanged() will only be emitted once for the latest available progress value and text.
To avoid a race condition, it is important to call this function after doing the connections.
See Also▲
See also future()
void QFutureWatcher::setPendingResultsLimit(int limit)▲
The setPendingResultsLimit() provides throttling control. When the number of pending resultReadyAt() or resultsReadyAt() signals exceeds the limit, the computation represented by the future will be throttled automatically. The computation will resume once the number of pending signals drops below the limit.
[since 6.0] void QFutureWatcher::setSuspended(bool suspend)▲
If suspend is true, this function suspends the asynchronous computation represented by the future(). If the computation is already suspended, this function does nothing. QFutureWatcher will not immediately stop delivering progress and result ready signals when the future is suspended. At the moment of suspending there may still be computations that are in progress and cannot be stopped. Signals for such computations will still be delivered.
If suspend is false, this function resumes the asynchronous computation. If the computation was not previously suspended, this function does nothing.
Be aware that not all computations can be suspended. For example, the QFuture returned by QtConcurrent::run() cannot be suspended; but the QFuture returned by QtConcurrent::mappedReduced() can.
This function was introduced in Qt 6.0.
See Also▲
See also suspended(), suspend(), resume(), toggleSuspended()
void QFutureWatcher::started()▲
This signal is emitted when this QFutureWatcher starts watching the future set with setFuture().
[since 6.0] void QFutureWatcher::suspend()▲
Suspends the asynchronous computation represented by this future. This is a convenience method that simply calls setSuspended(true).
This function was introduced in Qt 6.0.
See Also▲
See also resume()
[since 6.0] void QFutureWatcher::suspended()▲
This signal is emitted when suspend() took effect, meaning that there are no more running computations. After receiving this signal no more result ready or progress reporting signals are expected.
This function was introduced in Qt 6.0.
See Also▲
See also setSuspended(), suspend(), suspended()
[since 6.0] void QFutureWatcher::suspending()▲
This signal is emitted when the state of the watched future is set to suspended.
This signal only informs that suspension has been requested. It doesn't indicate that all background operations are stopped. Signals for computations that were in progress at the moment of suspending will still be delivered. To be informed when suspension actually took effect, use the suspended() signal.
This function was introduced in Qt 6.0.
See Also▲
See also setSuspended(), suspend(), suspended()
[since 6.0] void QFutureWatcher::toggleSuspended()▲
Toggles the suspended state of the asynchronous computation. In other words, if the computation is currently suspending or suspended, calling this function resumes it; if the computation is running, it is suspended. This is a convenience method for calling setSuspended(!(isSuspending() || isSuspended())).
This function was introduced in Qt 6.0.
See Also▲
See also setSuspended(), suspend(), resume()
void QFutureWatcher::waitForFinished()▲
Waits for the asynchronous computation to finish (including cancel()ed computations), i.e. until isFinished() returns true.
Obsolete Members for QFutureWatcher▲
The following members of class QFutureWatcher are deprecated. We strongly advise against using them in new code.
Obsolete Member Function Documentation▲
bool QFutureWatcher::isPaused() const▲
This function is deprecated since 6.0. We strongly advise against using it in new code.
Use isSuspending() or isSuspended() instead.
Returns true if the asynchronous computation has been paused with the pause() function; otherwise returns false.
Be aware that the computation may still be running even though this function returns true. See setPaused() for more details. To check if pause actually took effect, use isSuspended() instead.
See Also▲
See also setSuspended(), toggleSuspended(), isSuspended()
void QFutureWatcher::pause()▲
This function is deprecated. We strongly advise against using it in new code.
Use suspend() instead.
Pauses the asynchronous computation represented by the future(). This is a convenience method that simply calls setPaused(true).
See Also▲
See also resume()
void QFutureWatcher::paused()▲
This function is deprecated since 6.0. We strongly advise against using it in new code.
Use suspending() instead.
This signal is emitted when the state of the watched future is set to paused.
This signal only informs that pause has been requested. It doesn't indicate that all background operations are stopped. Signals for computations that were in progress at the moment of pausing will still be delivered. To to be informed when pause() actually took effect, use the suspended() signal.
See Also▲
See also setPaused(), setSuspended(), suspend(), suspended()
void QFutureWatcher::setPaused(bool paused)▲
This function is deprecated since 6.6. We strongly advise against using it in new code.
Use setSuspended() instead.
If paused is true, this function pauses the asynchronous computation represented by the future(). If the computation is already paused, this function does nothing. QFutureWatcher will not immediately stop delivering progress and result ready signals when the future is paused. At the moment of pausing there may still be computations that are in progress and cannot be stopped. Signals for such computations will still be delivered after pause.
If paused is false, this function resumes the asynchronous computation. If the computation was not previously paused, this function does nothing.
Be aware that not all computations can be paused. For example, the QFuture returned by QtConcurrent::run() cannot be paused; but the QFuture returned by QtConcurrent::mappedReduced() can.
See Also▲
See also paused(), suspend(), resume(), toggleSuspended()
void QFutureWatcher::togglePaused()▲
This function is deprecated since 6.0. We strongly advise against using it in new code.
Use toggleSuspended() instead.
Toggles the paused state of the asynchronous computation. In other words, if the computation is currently paused, calling this function resumes it; if the computation is running, it is paused. This is a convenience method for calling setPaused(!isPaused()).
See Also▲
See also setSuspended(), suspend(), resume()