QFutureWatcher 类

template <typename T> class QFutureWatcher

QFutureWatcher 类允许监视 QFuture 使用的信号和槽。 更多...

头: #include <QFutureWatcher>
qmake: QT += core
Since: Qt 4.4
继承: QObject

该类在 Qt 4.4 引入。

注意: 此类的所有函数 可重入 .

公共函数

QFutureWatcher (QObject * parent = nullptr)
virtual ~QFutureWatcher ()
QFuture<T> future () const
bool isCanceled () const
bool isFinished () const
bool isPaused () const
bool isRunning () const
bool isStarted () const
int progressMaximum () const
int progressMinimum () const
QString progressText () const
int progressValue () const
T result () const
T resultAt (int index ) const
void setFuture (const QFuture<T> & future )
void setPendingResultsLimit (int limit )
void waitForFinished ()

公共槽

void cancel ()
void pause ()
void resume ()
void setPaused (bool paused )
void togglePaused ()

信号

void canceled ()
void finished ()
void paused ()
void progressRangeChanged (int minimum , int maximum )
void progressTextChanged (const QString & progressText )
void progressValueChanged (int progressValue )
void resultReadyAt (int index )
void resultsReadyAt (int beginIndex , int endIndex )
void resumed ()
void started ()

详细描述

QFutureWatcher 提供信息和通知关于 QFuture 。使用 setFuture () 函数以开始看守特定 QFuture future () 函数返回未来设置采用 setFuture ().

为方便起见,几个 QFuture 函数也可用于 QFutureWatcher: progressValue (), progressMinimum (), progressMaximum (), progressText (), isStarted (), isFinished (), isRunning (), isCanceled (), isPaused (), waitForFinished (), result (),和 resultAt ()。 cancel (), setPaused (), pause (), resume (),和 togglePaused () 函数是槽在 QFutureWatcher。

Status changes are reported via the started (), finished (), canceled (), paused (), resumed (), resultReadyAt (),和 resultsReadyAt () signals. Progress information is provided from the progressRangeChanged (), void progressValueChanged (),和 progressTextChanged () 信号。

Throttling control is provided by the setPendingResultsLimit () function. When the number of pending resultReadyAt () 或 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<int> watcher;
connect(&watcher, &QFutureWatcher<int>::finished, &myObject, &MyClass::handleFinished);
// Start the computation.
QFuture<int> future = QtConcurrent::run(...);
watcher.setFuture(future);
					

Be aware that not all running asynchronous computations can be canceled or paused. 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.

另请参阅 QFuture and Qt Concurrent .

成员函数文档编制

QFutureWatcher:: QFutureWatcher ( QObject * parent = nullptr)

Constructs a new QFutureWatcher with the given parent . Until a future is set with setFuture (), the functions isStarted (), isCanceled (),和 isFinished () return true .

[slot] 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 (),和 resultsReadyAt () 信号。

Be aware that not all running asynchronous computations can be canceled. For example, the QFuture 返回通过 QtConcurrent::run () cannot be canceled; but the QFuture 返回通过 QtConcurrent::mappedReduced () can.

[signal] void QFutureWatcher:: canceled ()

This signal is emitted if the watched future is canceled.

[signal] void QFutureWatcher:: finished ()

This signal is emitted when the watched future finishes.

[slot] void QFutureWatcher:: pause ()

Pauses the asynchronous computation represented by the future (). This is a convenience method that simply calls setPaused (true)。

另请参阅 resume ().

[signal] void QFutureWatcher:: paused ()

This signal is emitted when the watched future is paused.

另请参阅 setPaused ().

[signal] void QFutureWatcher:: progressRangeChanged ( int minimum , int maximum )

The progress range for the watched future has changed to minimum and maximum

[signal] void QFutureWatcher:: progressTextChanged (const QString & progressText )

This signal is emitted when the watched future reports textual progress information, progressText .

[signal] 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.

[signal] 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);

[signal] 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 .

[slot] void QFutureWatcher:: resume ()

Resumes the asynchronous computation represented by the future (). This is a convenience method that simply calls setPaused (false).

另请参阅 pause ().

[signal] void QFutureWatcher:: resumed ()

This signal is emitted when the watched future is resumed.

[slot] void QFutureWatcher:: setPaused ( bool paused )

paused is true, this function pauses the asynchronous computation represented by the future (). If the computation is already paused, this function does nothing. This QFutureWatcher will stop delivering progress and result ready signals while the future is paused. Signal delivery will continue once the computation is resumed.

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 返回通过 QtConcurrent::run () cannot be paused; but the QFuture 返回通过 QtConcurrent::mappedReduced () can.

另请参阅 paused (), pause (), resume (),和 togglePaused ().

[signal] void QFutureWatcher:: started ()

This signal is emitted when this QFutureWatcher starts watching the future set with setFuture ().

[slot] void QFutureWatcher:: togglePaused ()

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 becomes paused. This is a convenience method for calling setPaused (! isPaused ()).

另请参阅 setPaused (), pause (),和 resume ().

[虚拟] QFutureWatcher:: ~QFutureWatcher ()

销毁 QFutureWatcher .

QFuture < T > QFutureWatcher:: future () const

Returns the watched future.

另请参阅 setFuture ().

bool QFutureWatcher:: isCanceled () const

返回 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 。见 cancel () 了解更多细节。

bool QFutureWatcher:: isFinished () const

返回 true if the asynchronous computation represented by the future () has finished, or if no future has been set; otherwise returns false .

bool QFutureWatcher:: isPaused () const

返回 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 。见 setPaused () 了解更多细节。

另请参阅 setPaused () 和 togglePaused ().

bool QFutureWatcher:: isRunning () const

返回 true if the asynchronous computation represented by the future () is currently running; otherwise returns false .

bool QFutureWatcher:: isStarted () const

返回 true if the asynchronous computation represented by the future () has been started, or if no future has been set; otherwise returns false .

int QFutureWatcher:: progressMaximum () const

返回最大 progressValue ().

另请参阅 progressValue () 和 progressMinimum ().

int QFutureWatcher:: progressMinimum () const

返回最小 progressValue ().

另请参阅 progressValue () 和 progressMaximum ().

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.

int QFutureWatcher:: progressValue () const

返回当前进度值,位于 progressMinimum () 和 progressMaximum ().

另请参阅 progressMinimum () 和 progressMaximum ().

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

另请参阅 resultAt ().

T QFutureWatcher:: resultAt ( int index ) const

Returns the result at index future (). If the result is not immediately available, this function will block and wait for the result to become available.

另请参阅 result ().

void QFutureWatcher:: setFuture (const QFuture < T > & future )

开始看守给定 future .

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 (), paused (), canceled (),和 finished (). Of these, resultsReadyAt () 和 resultReadyAt () may be emitted several times to cover all available results. progressValueChanged () 和 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.

另请参阅 future ().

void QFutureWatcher:: setPendingResultsLimit ( int limit )

The setPendingResultsLimit() provides throttling control. When the number of pending resultReadyAt () 或 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 .

void QFutureWatcher:: waitForFinished ()

Waits for the asynchronous computation to finish (including cancel ()ed computations).