本页内容

QNativeIpcKey Class

QNativeIpcKey 类保存了一个由QSystemSemaphore 和QSharedMemory 使用的本机密钥。更多内容...

头文件: #include <QNativeIpcKey>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
自: Qt 6.6

QNativeIpcKey 比较

类别可比较类型
相等性QNativeIpcKey

公共类型

enum class Type { SystemV, PosixRealtime, Windows }

公共函数

QNativeIpcKey()
QNativeIpcKey(QNativeIpcKey::Type type)
QNativeIpcKey(const QString &key, QNativeIpcKey::Type type = DefaultTypeForOs)
QNativeIpcKey(const QNativeIpcKey &other)
QNativeIpcKey(QNativeIpcKey &&other)
~QNativeIpcKey()
bool isEmpty() const
bool isValid() const
QString nativeKey() const
void setNativeKey(const QString &newKey)
void setType(QNativeIpcKey::Type type)
void swap(QNativeIpcKey &other)
QString toString() const
QNativeIpcKey::Type type() const
QNativeIpcKey &operator=(QNativeIpcKey &&other)
QNativeIpcKey &operator=(const QNativeIpcKey &other)

静态公共成员

const QNativeIpcKey::Type DefaultTypeForOs
QNativeIpcKey fromString(const QString &text)
QNativeIpcKey::Type legacyDefaultTypeForOs()
size_t qHash(const QNativeIpcKey &key, size_t seed = 0)
void swap(QNativeIpcKey &value1, QNativeIpcKey &value2)
bool operator!=(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs)
bool operator==(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs)

详细说明

QSharedMemory 和QSystemSemaphore 类使用一个称为“键”的系统级标识符来标识其资源。低级键值以及键类型在Qt中通过QNativeIpcKey 类进行封装。

这两个类还提供了使用 QSharedMemory::platformSafeKey() 和 QSystemSemaphore::platformSafeKey() 从跨平台标识符创建本机密钥的方法。应用程序绝不应共享这些函数的输入,因为不同版本的 Qt 可能会执行不同的转换,从而导致本机密钥不同。 相反,创建 IPC 对象的应用程序应通过下文所述的方法传递生成的本机密钥。

有关密钥类型、特定平台的限制以及与旧版或非 Qt 应用程序的互操作性的详细信息,请参阅《本机 IPC 密钥》文档。其中包含针对 Apple 平台上沙盒应用程序的重要信息,包括通过 Apple App Store 获取的所有应用程序。

将密钥传递给其他进程

向其他 Qt 进程传递密钥

如果另一个进程支持 QNativeIpcKey,则最佳的通信方式是通过toString() 获取字符串表示形式,并使用fromString() 对其进行解析。该表示形式可以存储在名称已知的文件中,或者使用QProcess::setArguments() 通过命令行传递给子进程。

如果另一个进程不支持 QNativeIpcKey,则两个进程可以交换nativeKey() 返回的字符串,但旧代码很可能无法调整其密钥类型。legacyDefaultTypeForOs() 函数会返回旧代码使用的类型,该类型可能与DefaultTypeForOs 常量不匹配。即使旧应用程序使用的构建版本与新应用程序不同(例如,它是 Qt 5 应用程序),只要传递给 Qt configure 脚本的选项相同,这一情况仍然成立。

向非Qt进程传递密钥

在与非 Qt 进程通信时,应用程序必须设法获取对方进程所使用的密钥类型。这一点在 Unix 系统上尤为重要,因为该系统中PosixRealtime 和SystemV 两种机制都很常见。

本机密钥的字符串表示形式

QNativeIpcKey 的字符串表示格式旨在保持稳定,因此只要相关 Qt 版本支持该密钥类型,便具有向前和向后兼容性。也就是说,较旧的 Qt 版本将无法解析在其发布之后引入的密钥类型的字符串表示形式。 但是,成功解析字符串表示形式并不意味着 Qt 类能够成功创建该类型的对象;应用程序应使用 QSharedMemory::isKeyTypeSupported() 和 QSystemSemaphore::isKeyTypeSupported() 验证支持情况。

字符串表示形式的格式由两个部分组成,两者以冒号(':')分隔。第一部分是键类型,如下表所述。第二部分是特定于该类型的有效载荷,采用percent-encoding 格式。对于所有当前支持的键类型,解码后的形式与nativeKey()字段的内容完全一致。

密钥类型字符串表示形式
PosixRealtime"posix"
SystemV"systemv"
Windows"windows"
非标准SystemV"systemv-" 后跟一个十进制数

此格式类似于 URI,允许使用 URI/URL 解析函数(如QUrl )进行解析。当通过此类 API 解析时,键类型将显示为scheme ,而有效载荷将为path 。查询字符串或片段的使用已被保留。

另请参阅 QSharedMemory 和QSystemSemaphore 。

成员类型文档

enum class QNativeIpcKey::Type

此枚举描述了 IPC 对象的后端类型。有关键值的详细信息,请参阅《本机 IPC 键》文档。

常量值描述
QNativeIpcKey::Type::SystemV0x51X/Open 系统倡议 (XSI) 或 System V (SVr4) API
QNativeIpcKey::Type::PosixRealtime0x100IEEE 1003.1b(POSIX.1b)API
QNativeIpcKey::Type::Windows0x101Win32 API

另请参阅 setType() 和type()。

成员函数文档

[constexpr noexcept default] QNativeIpcKey::QNativeIpcKey()

创建一个类型为DefaultTypeForOs 、键为空的QNativeIpcKey对象。

[explicit constexpr noexcept] QNativeIpcKey::QNativeIpcKey(QNativeIpcKey::Type type)

QNativeIpcKey::QNativeIpcKey(const QString &key, QNativeIpcKey::Type type = DefaultTypeForOs)

为类型type 构建一个包含本机键key (或在无参数的重载中为空)的QNativeIpcKey 对象。

[noexcept] QNativeIpcKey::~QNativeIpcKey()

释放此QNativeIpcKey 对象。

[static] QNativeIpcKey QNativeIpcKey::fromString(const QString &text)

解析字符串形式的text ,并返回相应的QNativeIpcKey 。字符串表示形式可用于通知其他进程本进程创建的键以及它们应挂载到的键。

如果无法解析该字符串,则该函数返回一个invalid 对象。

另请参阅 toString() 和isValid()。

[noexcept] bool QNativeIpcKey::isEmpty() const

如果nativeKey()为空,则返回true。

另请参阅 nativeKey()。

[noexcept] bool QNativeIpcKey::isValid() const

如果该对象包含有效的本机IPC键类型,则返回true。无效类型通常是由于无法使用fromString()解析字符串表示形式所致。

该函数不会检查该键字符串是否实际受当前操作系统支持或是否有效。

另请参阅 type() 和fromString()。

[static noexcept] QNativeIpcKey::Type QNativeIpcKey::legacyDefaultTypeForOs()

返回与QSharedMemory 和QSystemSemaphore 在Qt 6.6之前使用的原生IPC键对应的Type 。必须与使用上述任一类且使用Qt 6.6之前版本编译的代码保持兼容的应用程序和库,可以使用此函数来确定其他应用程序可能使用的IPC类型。

请注意,此函数依赖于 Qt 在构建时使用了完全相同的 configure 选项。

[noexcept] QString QNativeIpcKey::nativeKey() const

返回与该对象关联的原生密钥字符串。

另请参阅 setNativeKey() 和type()。

void QNativeIpcKey::setNativeKey(const QString &newKey)

将此对象的原生键设置为newKey 。

另请参阅 nativeKey() 和setType()。

[constexpr] void QNativeIpcKey::setType(QNativeIpcKey::Type type)

将此对象的IPC类型设置为type 。

另请参阅 type() 和setNativeKey()。

[noexcept] void QNativeIpcKey::swap(QNativeIpcKey &other)

将此本机IPC键和类型与other 互换。此操作速度极快,且绝不会失败。

QString QNativeIpcKey::toString() const

返回该对象的字符串表示形式。字符串表示形式可用于告知其他进程本进程创建的键,以及它们应挂载到该键上。

如果当前对象是invalid ,则该函数返回空字符串。

另请参阅 fromString()。

[constexpr noexcept] QNativeIpcKey::Type QNativeIpcKey::type() const

返回与该对象关联的键类型。

另请参阅 ` nativeKey()` 和 `setType()`。

[noexcept] QNativeIpcKey &QNativeIpcKey::operator=(QNativeIpcKey &&other)

QNativeIpcKey &QNativeIpcKey::operator=(const QNativeIpcKey &other)

[noexcept] QNativeIpcKey::QNativeIpcKey(QNativeIpcKey &&other)

QNativeIpcKey::QNativeIpcKey(const QNativeIpcKey &other)

复制或移动other 中的内容。

成员变量文档

const QNativeIpcKey::Type QNativeIpcKey::DefaultTypeForOs

该常量表达式变量存储了当前操作系统的默认本机 IPC 类型。对于 Windows 系统,其值为 `Type::Windows `;对于其他系统,其值为 `Type::PosixRealtime `。请注意,该常量与 Qt 6.6 之前大多数 Unix 系统中 `QSharedMemory ` 和 `QSystemSemaphore ` 的默认值不同;更多信息请参阅legacyDefaultTypeForOs()。

相关的非成员

[noexcept] size_t qHash(const QNativeIpcKey &key, size_t seed = 0)

返回key 的哈希值,并使用seed 作为计算的初始值。

[noexcept] void swap(QNativeIpcKey &value1, QNativeIpcKey &value2)

将原生IPC键及其类型value1 与value2 互换。此操作速度极快,且绝不会失败。

[noexcept] bool operator!=(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs)

[noexcept] bool operator==(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs)

如果 `lhs ` 和 `rhs ` 对象的内容相同(或不同),则返回 true。

© 2026 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.