QRunnable 类

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

头: #include <QRunnable>
CMake: find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

公共函数

  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 在单独线程中执行代码。 QThreadPool deletes the QRunnable automatically if autoDelete () 返回 true (默认)。使用 setAutoDelete () 以改变自动删除标志。

QThreadPool supports executing the same QRunnable more than once by calling QThreadPool::tryStart (this) 从 run () 函数。若 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 被启用会创建竞争条件且不推荐。

另请参阅 QThreadPool .

成员函数文档编制

QRunnable:: QRunnable ()

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

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

[virtual] QRunnable:: ~QRunnable ()

QRunnable 虚拟析构函数。

bool QRunnable:: autoDelete () const

返回 true 自动删除被启用;否则 false。

若自动删除被启用, QThreadPool 将自动删除此可运行后于调用 run ();否则,所有权仍然属于应用程序程序员。

另请参阅 setAutoDelete () 和 QThreadPool .

[static, since 5.15] 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 ()

在子类中实现此纯虚函数。

void QRunnable:: setAutoDelete ( bool autoDelete )

启用自动删除若 autoDelete 为 true;否则自动删除被禁用。

若自动删除被启用, QThreadPool 将自动删除此可运行后于调用 run ();否则,所有权仍然属于应用程序程序员。

注意,必须设置此标志先于调用 QThreadPool::start ()。调用此函数后于 QThreadPool::start () 将导致未定义行为。

另请参阅 autoDelete () 和 QThreadPool .