void long_task() {
forever {
if ( QThread::currentThread()->isInterruptionRequested() ) {
return;
Note: This can only be called within the thread itself, i.e. when it is the current thread.
See also currentThread() and requestInterruption().
bool QThread::isMainThread()
Returns whether the currently executing thread is the main thread.
The main thread is the thread in which QCoreApplication was created. This is usually the thread that called the main()
function, but not necessarily so. It is the thread that is processing the GUI events and in which graphical objects (QWindow, QWidget) can be created.
This function was introduced in Qt 6.8.
See also currentThread() and QCoreApplication::instance().
bool QThread::isRunning() const
Returns true
if the thread is running; otherwise returns false
.
Note: This function is thread-safe.
See also isFinished().
int QThread::loopLevel() const
Returns the current event loop level for the thread.
Note: This can only be called within the thread itself, i.e. when it is the current thread.
This is an overloaded function, equivalent to calling:
QThread::sleep(std::chrono::milliseconds{msecs});
Note: This function does not guarantee accuracy. The application may sleep longer than msecs under heavy load conditions. Some OSes might round msecs up to 10 ms or 15 ms.
See also sleep() and usleep().
Returns the priority for a running thread. If the thread is not running, this function returns InheritPriority
.
See also Priority, setPriority(), and start().
void QThread::quit()
Tells the thread's event loop to exit with return code 0 (success). Equivalent to calling QThread::exit(0).
This function does nothing if the thread does not have an event loop.
Note: This function is thread-safe.
See also exit() and QEventLoop.
void QThread::requestInterruption()
Request the interruption of the thread. That request is advisory and it is up to code running on the thread to decide if and how it should act upon such request. This function does not stop any event loop running on the thread and does not terminate it in any way.
Note: This function is thread-safe.
See also isInterruptionRequested().
void QThread::run()
The starting point for the thread. After calling start(), the newly created thread calls this function. The default implementation simply calls exec().
You can reimplement this function to facilitate advanced thread management. Returning from this method will end the execution of the thread.
See also start() and wait().
void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
Sets the event dispatcher for the thread to eventDispatcher. This is only possible as long as there is no event dispatcher installed for the thread yet.
An event dispatcher is automatically created for the main thread when QCoreApplication is instantiated and on start() for auxiliary threads.
This method takes ownership of the object.
See also eventDispatcher().
void QThread::setPriority(QThread::Priority priority)
This function sets the priority for a running thread. If the thread is not running, this function does nothing and returns immediately. Use start() to start a thread with a specific priority.
The priority argument can be any value in the QThread::Priority
enum except for InheritPriority
.
The effect of the priority parameter is dependent on the operating system's scheduling policy. In particular, the priority will be ignored on systems that do not support thread priorities (such as on Linux, see http://linux.die.net/man/2/sched_setscheduler for more details).
See also Priority, priority(), and start().
void QThread::setStackSize(uint stackSize)
Sets the stack size for the thread to stackSize. If stackSize is zero, the operating system or runtime will choose a default value. Otherwise, the thread's stack size will be the value provided (which may be rounded up or down).
On most operating systems, the amount of memory allocated to serve the stack will initially be smaller than stackSize and will grow as the thread uses the stack. This parameter sets the maximum size it will be allowed to grow to (that is, it sets the size of the virtual memory space the stack is allowed to occupy).
This function can only be called before the thread is started.
Warning: Most operating systems place minimum and maximum limits on thread stack sizes. The thread will fail to start if the stack size is outside these limits.
See also stackSize().
void QThread::setTerminationEnabled(bool enabled = true)
Enables or disables termination of the current thread based on the enabled parameter. The thread must have been started by QThread.
When enabled is false, termination is disabled. Future calls to QThread::terminate() will return immediately without effect. Instead, the termination is deferred until termination is enabled.
When enabled is true, termination is enabled. Future calls to QThread::terminate() will terminate the thread normally. If termination has been deferred (i.e. QThread::terminate() was called with termination disabled), this function will terminate the calling thread immediately. Note that this function will not return in this case.
See also terminate().
void QThread::sleep(std::chrono::nanoseconds nsecs)
Forces the current thread to sleep for nsecs.
Avoid using this function if you need to wait for a given condition to change. Instead, connect a slot to the signal that indicates the change or use an event handler (see QObject::event()).
Note: This function does not guarantee accuracy. The application may sleep longer than nsecs under heavy load conditions.
This function was introduced in Qt 6.6.
void QThread::sleep(unsigned long secs)
Forces the current thread to sleep for secs seconds.
This is an overloaded function, equivalent to calling:
QThread::sleep(std::chrono::seconds{secs});
See also msleep() and usleep().
uint QThread::stackSize() const
Returns the maximum stack size for the thread (if set with setStackSize()); otherwise returns zero.
See also setStackSize().
void QThread::start(QThread::Priority priority = InheritPriority)
Begins execution of the thread by calling run(). The operating system will schedule the thread according to the priority parameter. If the thread is already running, this function does nothing.
The effect of the priority parameter is dependent on the operating system's scheduling policy. In particular, the priority will be ignored on systems that do not support thread priorities (such as on Linux, see the sched_setscheduler documentation for more details).
See also run() and terminate().
void QThread::started()
This signal is emitted from the associated thread when it starts executing, so any slots connected to it may be called via queued invocation. Whilst the event may have been posted before run() is called, any cross-thread delivery of the signal may still be pending.
Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.
See also run() and finished().
void QThread::terminate()
Terminates the execution of the thread. The thread may or may not be terminated immediately, depending on the operating system's scheduling policies. Use QThread::wait() after terminate(), to be sure.
When the thread is terminated, all threads waiting for the thread to finish will be woken up.
Warning: This function is dangerous and its use is discouraged. The thread can be terminated at any point in its code path. Threads can be terminated while modifying data. There is no chance for the thread to clean up after itself, unlock any held mutexes, etc. In short, use this function only if absolutely necessary.
Termination can be explicitly enabled or disabled by calling QThread::setTerminationEnabled(). Calling this function while termination is disabled results in the termination being deferred, until termination is re-enabled. See the documentation of QThread::setTerminationEnabled() for more information.
Note: This function is thread-safe.
See also setTerminationEnabled().
void QThread::usleep(unsigned long usecs)
This is an overloaded function, equivalent to calling:
QThread::sleep(std::chrono::microseconds{secs});
Note: This function does not guarantee accuracy. The application may sleep longer than usecs under heavy load conditions. Some OSes might round usecs up to 10 ms or 15 ms; on Windows, it will be rounded up to a multiple of 1 ms.
See also sleep() and msleep().
bool QThread::wait(QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever))
Blocks the thread until either of these conditions is met:
as published by the Free Software Foundation. Qt and respective logos are
of The Qt Company Ltd. in Finland and/or other countries
worldwide. All other trademarks are property of their respective owners.