QRunnable Class
The QRunnable class is the base class for all runnable objects. More…
| Header | #include <QRunnable> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Functions
| QRunnable() | |
| virtual | ~QRunnable() |
| virtual void | run() |
| bool | autoDelete() const |
| void | setAutoDelete(bool autoDelete) |
Static Public Members
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.
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().
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> 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.
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().