进程间通信

Qt supports many ways of communicating with other processes running in the same system or in different systems. There are basically three types of inter-process communication mechanisms:

  1. 同步原语
  2. 任意字节级数据的交换
  3. 传递结构化消息

同步原语

Qt only provides one class for explicit inter-process synchronization: QSystemSemaphore QSystemSemaphore is like a QSemaphore that is accessible by multiple processes in the same system. It is globally identified by a "key", which in Qt is represented by the QNativeIpcKey class. Additionally, depending on the OS, Qt may support multiple different backends for sharing memory; see the Native IPC Keys documentation for more information and limitations.

It is possible to use regular thread-synchronization primitives such as mutexes, wait conditions, and read-write locks, located in memory that is shared between processes. Qt does not provide any class to support this, but applications can use low-level operations on certain operating systems.

Other Qt classes may be used to provide higher-level locking, like QLockFile , or by acquiring a unique, system-wide resource. Such techniques include TCP or UDP ports or well-known names in D-Bus .

字节级数据共享

Using byte-level data, applications can implement any communication protocol they may choose. Sharing of byte data can be stream-oriented (serialized) or can allow random access (a similar condition to QFileDevice::isSequential ()).

For serial communication, Qt provides a number of different classes and even full modules:

For random-access data sharing within the same system, Qt provides QSharedMemory 。见 共享内存 documentation for detailed information.

结构化消息传递

Qt also provides a number of techniques to exchange structured messages with other processes. Applications can build on top of the byte-level solutions above, such as by using QJsonDocument or QXmlStreamReader / QXmlStreamWriter over HTTP to perform JSONRPC or XMLRPC, respectively, or QCborValue with QtCoAP.

Dedicated Qt modules for structured messages and remote procedure-calling include: