Home > Articles > Open Source > Python

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Qt Threading

Qt is a large but well-designed and well-documented library. Originally, it just provided the wherewithal to create GUIs—buttons, windows, and so on. Then, bit by bit, other features were added: networking classes, SQL classes, generic data containers. A String class has always been part of Qt. And now there are thread classes. Qt's thread classes have been well designed and are easy to use.

But you still have to keep in mind that classes that derive from QObject (such as all widgets) are not thread-safe. The same holds for classes that deal with the OS, such as QProcess and QSocket. The class QRegExp uses static data, and you cannot even create different instances for different threads. The same problem holds with Python's standard random module and Python's own threads, by the way.

Qt offers the following four classes:

  • QThread—Encapsulates a platform thread

  • QMutex—Serializes access to data between threads

  • QSemaphore—A mutex that can be accessed from more than one thread at a time

  • QWaitCondition—Allows a thread to signal to one or more other threads that are waiting

Apart from these classes, the class QApplication offers five functions that allow you to serialize access to the GUI thread:

  • lock()—Locks the GUI thread. If the GUI thread is currently locked, waits to obtain the lock.

  • unlock()—Unlocks the GUI thread.

  • locked()—Determines whether the GUI thread is currently locked.

  • tryLock()—Tries to lock, but returns if it's not possible.

  • wakeUpGUIThread()—Wakes up the GUI thread.

  • guiThreadAwake()—Determines whether the GUI thread is awake.

  • + Share This
  • 🔖 Save To Your Account