6.0.0-rc4Source

QRunnable Class

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

Header#include <QRunnable>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QRunnable()
virtual ~QRunnable()
virtual voidrun()
boolautoDelete() const
voidsetAutoDelete(bool autoDelete)

Static Public Members

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

Detailed Description

A QRunnable is a task for a thread pool: subclass it, reimplement run() and hand the object to QThreadPool::start(). The pool runs it in one of its threads and, if autoDelete() is true, deletes it afterwards.

class Hello : public QRunnable {
public:
    void run() override { qDebug() << "Hello from a pool thread"; }
};
QThreadPool::globalInstance()->start(new Hello);

See also QThreadPool.

Differences from Qt

None.

Member Function Documentation

QRunnable::QRunnable()

Constructs a runnable that the thread pool deletes after it ran.

[virtual] QRunnable::~QRunnable()

Destroys the runnable.

bool QRunnable::autoDelete() const

Returns true if the thread pool deletes this object after run() returns. The default is true.

See also setAutoDelete().

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

Creates a QRunnable that calls functionToRun from run(). The runnable has autoDelete() set to true, so the thread pool that runs it deletes it. The caller owns the object until it is passed to a pool.

See also QThreadPool::start().

template <typename Callable, QtPrivate::IfRunnableCallable<Callable> = true>[static] QRunnable * QRunnable::create(Callable &&functionToRun)

This is an overloaded function.

Creates a QRunnable that calls functionToRun, which may be a move-only callable such as a lambda that owns a std::unique_ptr. The callable is moved into the runnable.

This function was introduced in 6.0.

[virtual] void QRunnable::run()

Implement this pure virtual function in your subclass. It is called in a thread of the pool that runs the object.

void QRunnable::setAutoDelete(bool autoDelete)

Sets whether the thread pool deletes this object after run() returns to autoDelete. Change it before the runnable is passed to a pool; changing it afterwards has an undefined effect.

See also autoDelete().