QNativeIpcKey Class

QNativeIpcKey 类持有QSystemSemaphoreQSharedMemory 使用的本地密钥。更多

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

该类可等价比较

公共类型

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)

详细说明

QSharedMemoryQSystemSemaphore 类使用称为 "key "的全系统标识符来标识其资源。低级键值以及键类型通过QNativeIpcKey 类封装在 Qt 中。

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

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

将密钥传递给其他进程

与其他 Qt 进程通信密钥

如果其他进程支持 QNativeIpcKey,最好的通信方式是通过toString() 获取字符串表示并使用fromString() 进行解析。该表示法可以存储在名称众所周知的文件中,也可以使用QProcess::setArguments() 通过命令行传递给子进程。

如果另一个进程不支持 QNativeIpcKey,那么两个进程可以交换nativeKey() ,但旧代码很可能无法调整其键类型。legacyDefaultTypeForOs() 函数会返回旧代码使用的类型,这可能与DefaultTypeForOs 常量不匹配。如果传递给 Qt configure 脚本的选项相同,即使旧程序使用的构建版本与新程序不同(例如,它是一个 Qt 5 程序),情况也是如此。

与非 Qt 进程通信密钥

与非 Qt 进程通信时,应用程序必须安排获取其他进程正在使用的密钥类型。这一点在 Unix 系统中尤为重要,因为在该系统中PosixRealtimeSystemV 都很常见。

本地键的字符串表示

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 。 保留使用查询或片段。

另请参阅 QSharedMemoryQSystemSemaphore

成员类型文档

enum class QNativeIpcKey::Type

该枚举描述 IPC 对象的后端类型。有关键类型的详细信息,请参阅本地 IPC 键文档。

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

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

成员函数文档

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

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

type 类型构造一个QNativeIpcKey 对象,其中包含本地键key (或在重载时为空,不含参数)。

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

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

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

QNativeIpcKey::QNativeIpcKey(const QNativeIpcKey &other)

复制或移动other 的内容。

[constexpr noexcept] QNativeIpcKey::QNativeIpcKey()

构造一个 QNativeIpcKey 对象,其类型为DefaultTypeForOs ,键值为空。

[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()

返回与QSharedMemoryQSystemSemaphore 在 Qt 6.6 之前使用的本地 IPC 键相对应的Type 。如果应用程序和程序库必须与使用这两个类的代码保持兼容,而这些代码是用 6.6 版之前的 Qt 编译的,则可以使用此函数来确定其他应用程序可能使用的 IPC 类型。

请注意,该函数依赖于 Qt 在编译时使用了相同的 configure-time 选项。

[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()。

成员变量文档

const QNativeIpcKey::Type QNativeIpcKey::DefaultTypeForOs

该常量表达式变量保存当前操作系统的默认本地 IPC 类型。Windows 系统的默认值为Type::Windows ,其他系统的默认值为Type::PosixRealtime 。请注意,该常量不同于QSharedMemoryQSystemSemaphore 在 Qt XML 6.6 之前的大多数 Unix 系统中的默认值;更多信息请参阅legacyDefaultTypeForOs()。

相关非会员

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

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

如果lhsrhs 对象的内容相同(或不同),则返回 true。

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

返回key 的哈希值,使用seed 作为计算的种子。

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

将本地 IPC 键和value1 类型与value2 互换。该操作速度非常快,从未出现过故障。

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