QRunnable Class

The QRunnable class is the base class for all runnable objects. More...

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

Public Functions

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

Static Public Members

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

Detailed Description

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

You can use QThreadPool to execute your code in a separate thread. QThreadPool deletes the QRunnable automatically if autoDelete() returns true (the default). Use setAutoDelete() to change the auto-deletion flag.

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.

See also QThreadPool.

Member Function Documentation

[constexpr] QRunnable::QRunnable()

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

See also autoDelete() and setAutoDelete().

[virtual] QRunnable::~QRunnable()

QRunnable virtual destructor.

bool QRunnable::autoDelete() const

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

See also setAutoDelete() and QThreadPool.

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

Creates a QRunnable that calls functionToRun in run().

Auto-deletion is enabled by default.

See also run() and 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.

See also autoDelete() and QThreadPool.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.