Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QThread Class Reference

Inheritance diagram for QThread:


List of all members.

Detailed Description

The QThread class provides platform-independent threads.

A QThread represents a separate thread of control within the program; it shares data with all the other threads within the process but executes independently in the way that a separate program does on a multitasking operating system. Instead of starting in main(), QThreads begin executing in run(). To create your own threads, subclass QThread and reimplement run(). For example:

        class MyThread : public QThread
            void run();

        void MyThread::run()
            QTcpSocket socket;
            // connect QTcpSocket's signals somewhere meaningful
            socket.connectToHost(hostName, portNumber);

This will create a QTcpSocket in the thread and then execute the thread's event loop. Use the start() method to begin execution. Execution ends when you return from run(), just as an application does when it leaves main(). QThread will notifiy you via a signal when the thread is started(), finished(), and terminated(), or you can use isFinished() and isRunning() to query the state of the thread. Use wait() to block until the thread has finished execution.

Each thread gets its own stack from the operating system. The operating system also determines the default size of the stack. You can use setStackSize() to set a custom stack size.

Each QThread can have its own event loop. You can start the event loop by calling exec(); you can stop it by calling exit() or quit(). Having an event loop in a thread makes it possible to connect signals from other threads to slots in this threads, using a mechanism called {Qt::QueuedConnection}{queued connections}. It also makes it possible to use classes that require the event loop, such as QTimer and QTcpSocket, in the thread.

In extreme cases, you may want to forcibly terminate() an executing thread. However, doing so is dangerous and discouraged. Please read the documentation for terminate() and setTerminationEnabled() for detailed information.

The static functions currentThreadId() and currentThread() return identifiers for the currently executing thread. The former returns a platform specific ID for the thread; the latter returns a QThread pointer.

QThread also provides platform independent sleep functions in varying resolutions. Use sleep() for full second resolution, msleep() for millisecond resolution, and usleep() for microsecond resolution.

See also:
{Thread Support in Qt}, QThreadStorage, QMutex, QSemaphore, QWaitCondition, {Mandelbrot Example}, {Semaphores Example}, {Wait Conditions Example}

Definition at line 59 of file qthread.h.

Public Types

enum  Priority {
  IdlePriority, LowestPriority, LowPriority, NormalPriority,
  HighPriority, HighestPriority, TimeCriticalPriority, InheritPriority

Public Slots

void deleteLater ()
void quit ()
void start (Priority=InheritPriority)
void terminate ()


void destroyed (QObject *=0)
void finished ()
void started ()
void terminated ()

Public Member Functions

bool blockSignals (bool b)
const QObjectListchildren () const
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
bool disconnect (const QObject *receiver, const char *member=0)
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
void dumpObjectInfo ()
void dumpObjectTree ()
QList< QByteArraydynamicPropertyNames () const
virtual bool event (QEvent *)
virtual bool eventFilter (QObject *, QEvent *)
void exit (int retcode=0)
template<typename T>
findChild (const QString &name=QString()) const
template<typename T>
QList< T > findChildren (const QRegExp &re) const
template<typename T>
QList< T > findChildren (const QString &name=QString()) const
bool inherits (const char *classname) const
void installEventFilter (QObject *)
bool isFinished () const
bool isRunning () const
bool isWidgetType () const
void killTimer (int id)
void moveToThread (QThread *thread)
QString objectName () const
QObjectparent () const
Priority priority () const
QVariant property (const char *name) const
 QThread (QObject *parent=0)
void removeEventFilter (QObject *)
void setObjectName (const QString &name)
void setParent (QObject *)
void setPriority (Priority priority)
bool setProperty (const char *name, const QVariant &value)
void setStackSize (uint stackSize)
void setUserData (uint id, QObjectUserData *data)
bool signalsBlocked () const
uint stackSize () const
int startTimer (int interval)
QThreadthread () const
QObjectUserData * userData (uint id) const
bool wait (unsigned long time=ULONG_MAX)
 ~QThread ()

Static Public Member Functions

static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
static QThreadcurrentThread ()
static Qt::HANDLE currentThreadId ()
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
static int idealThreadCount ()
static uint registerUserData ()

Protected Member Functions

virtual void childEvent (QChildEvent *)
virtual void connectNotify (const char *signal)
virtual void customEvent (QEvent *)
virtual void disconnectNotify (const char *signal)
int exec ()
 QThread (QThreadPrivate &dd, QObject *parent=0)
int receivers (const char *signal) const
virtual void run ()=0
QObjectsender () const
virtual void timerEvent (QTimerEvent *)

Static Protected Member Functions

static void msleep (unsigned long)
static void setTerminationEnabled (bool enabled=true)
static void sleep (unsigned long)
static void usleep (unsigned long)

Protected Attributes

QObjectData * d_ptr

Static Protected Attributes

static const QMetaObject staticQtMetaObject


QString objectName
 the name of this object

Static Private Member Functions

static void cleanup ()
static void initialize ()


class QApplication
class QApplicationPrivate
class QCoreApplication
class QCoreApplicationPrivate
struct QMetaObject
class QThreadData
class QWidget

Related Functions

(Note that these are not member functions.)

qFindChild (const QObject *obj, const QString &name=QString(), T dummy=0)
qFindChild (const QObject *obj, const QString &name)
QList< T > qFindChildren (const QObject *obj, const QString &name=QString(), T dummy=0)
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
QList< T > qFindChildren (const QObject *obj, const QString &name)
T * qobject_cast (QObject *object)
void * qt_find_obj_child (QObject *parent, const char *type, const QString &name)

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index