QRunnable Class

The QRunnable class is the base class for all runnable objects. 更多...

头: #include <QRunnable>
qmake: QT += core
Since: Qt 4.4

该类在 Qt 4.4 引入。

公共函数

QRunnable ()
virtual ~QRunnable ()
bool autoDelete () const
virtual void run () = 0
void setAutoDelete (bool autoDelete )

静态公共成员

QRunnable * create (std::function<void ()> functionToRun )

详细描述

The QRunnable class is an interface for representing a task or piece of code that needs to be executed, represented by your reimplementation of the run () 函数。

可以使用 QThreadPool to execute your code in a separate thread. QThreadPool deletes the QRunnable automatically if autoDelete () 返回 true (the default). Use setAutoDelete () 以改变自动删除标志。

QThreadPool supports executing the same QRunnable more than once by calling QThreadPool::tryStart (this) from within the run () function. If autoDelete is enabled the QRunnable will be deleted when the last thread exits the run function. Calling QThreadPool::start () multiple times with the same QRunnable when autoDelete is enabled creates a race condition and is not recommended.

另请参阅 QThreadPool .

成员函数文档编制

QRunnable:: QRunnable ()

Constructs a QRunnable. Auto-deletion is enabled by default.

另请参阅 autoDelete () 和 setAutoDelete ().

[virtual] QRunnable:: ~QRunnable ()

QRunnable virtual destructor.

bool QRunnable:: autoDelete () const

返回 true is auto-deletion is enabled; false otherwise.

If auto-deletion is enabled, QThreadPool will automatically delete this runnable after calling run (); otherwise, ownership remains with the application programmer.

另请参阅 setAutoDelete () 和 QThreadPool .

[static] QRunnable *QRunnable:: create ( std::function < void ()> functionToRun )

创建 QRunnable that calls functionToRun in run ().

Auto-deletion is enabled by default.

该函数在 Qt 5.15 引入。

另请参阅 run () 和 autoDelete ().

[pure virtual] void QRunnable:: run ()

Implement this pure virtual function in your subclass.

void QRunnable:: setAutoDelete ( bool autoDelete )

Enables auto-deletion if autoDelete is true; otherwise auto-deletion is disabled.

If auto-deletion is enabled, QThreadPool will automatically delete this runnable after calling run (); otherwise, ownership remains with the application programmer.

Note that this flag must be set before calling QThreadPool::start (). Calling this function after QThreadPool::start () results in undefined behavior.

另请参阅 autoDelete () 和 QThreadPool .