Qt Cross Thread Signal Slot

  1. Qt Signals And Slots Tutorial
  2. Qt Signal Slot Threads
Qt signal slot thread

Qt provides several ways to implement Inter-Process Communication (IPC) in Qt applications.

Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type. Dec 02, 2011  Qt documentation states that signals and slots can be direct, queued and auto. It also stated that if object that owns slot 'lives' in a thread different from object that owns signal, emitting such signal will be like posting message - signal emit will return instantly and slot method will be called in target thread's event loop.

TCP/IP

The cross-platform Qt Network module provides classes that make network programming portable and easy. It offers high-level classes (e.g., QNetworkAccessManager, QFtp) that communicate using specific application-level protocols, and lower-level classes (e.g., QTcpSocket, QTcpServer, QSslSocket) for implementing protocols.

Shared Memory

Qt Signals And Slots Tutorial

The cross-platform shared memory class, QSharedMemory, provides access to the operating system's shared memory implementation. It allows safe access to shared memory segments by multiple threads and processes. Additionally, QSystemSemaphore can be used to control access to resources shared by the system, as well as to communicate between processes.

D-Bus protocol

The Qt D-Bus module is a Unix-only library you can use to implement IPC using the D-Bus protocol. It extends Qt's Signals and Slots mechanism to the IPC level, allowing a signal emitted by one process to be connected to a slot in another process. The Qt D-Bus documentation has detailed information on how to use the Qt D-Bus module.

QProcess Class

The cross-platform class QProcess can be used to start external programs as child processes, and to communicate with them. It provides an API for monitoring and controlling the state of the child process. QProcess gives access to the input/output channels of child process by inheriting from QIODevice.

Session Management

In Linux/X11 platforms, Qt provides support for session management. Sessions allow events to be propagated to processes, for example, to notify when a shutdown occurs. The process and applications can then perform any necessary operations such as save open documents.

© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

I have an object a created in the main thread, where its signals are connected to slots of other object, b, that also lives in the main thread.

Qt Signal Slot Threads

Then I create an object c and move it to a worker thread, where it will periodically access object a through a pointer, change its state and emit its signals.

Here is the code:

(Note: some lines were purposely ignored to keep the example short)

I hope a signals, emitted from thread worker, will trigger b slots in main thread. And I hope I'll have no concurrency problems because a members are 8-bit sized.

But c, that lives in thread worker, will be posting events in the main thread event queue. I'm not using any synchronization primitive, so I'm afraid I may run into problems.

May I use the signal/slot mechanism the way I'm using in the code example? Am I doing something wrong?