QNativeIpcKey Class

The QNativeIpcKey class holds a native key used by QSystemSemaphore and QSharedMemory. More...

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

Public Types

enum class Type { SystemV, PosixRealtime, Windows }

Public Functions

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)

Static Public Members

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

Detailed Description

The QSharedMemory and QSystemSemaphore classes identify their resource using a system-wide identifier known as a "key". The low-level key value as well as the key type are encapsulated in Qt using the QNativeIpcKey class.

Those two classes also provide the means to create native keys from a cross-platform identifier, using QSharedMemory::platformSafeKey() and QSystemSemaphore::platformSafeKey(). Applications should never share the input to those functions, as different versions of Qt may perform different transformations, resulting in different native keys. Instead, the application that created the IPC object should communicate the resulting native key using the methods described below.

For details on the key types, platform-specific limitations, and interoperability with older or non-Qt applications, see the Native IPC Keys documentation. That includes important information for sandboxed applications on Apple platforms, including all apps obtained via the Apple App Store.

Communicating keys to other processes

Communicating keys to other Qt processes

If the other process supports QNativeIpcKey, the best way of communicating is via the string representation obtained from toString() and parsing it using fromString(). This representation can be stored on a file whose name is well-known or passed on the command-line to a child process using QProcess::setArguments().

If the other process does not support QNativeIpcKey, then the two processes can exchange the nativeKey() but the older code is likely unable to adjust its key type. The legacyDefaultTypeForOs() function returns the type that legacy code used, which may not match the DefaultTypeForOs constant. This is still true even if the old application is not using the same build as the new one (for example, it is a Qt 5 application), provided the options passed to the Qt configure script are the same.

Communicating keys to non-Qt processes

When communicating with non-Qt processes, the application must arrange to obtain the key type the other process is using. This is important particularly on Unix systems, where both PosixRealtime and SystemV are common.

String representation of native keys

The format of the string representation of a QNativeIpcKey is meant to be stable and therefore backwards and forwards compatible, provided the key type is supported by the Qt version in question. That is to say, an older Qt will fail to parse the string representation of a key type introduced after it was released. However, successfully parsing a string representation does not imply the Qt classes can successfully create an object of that type; applications should verify support using QSharedMemory::isKeyTypeSupported() and QSystemSemaphore::isKeyTypeSupported().

The format of the string representation is formed by two components, separated by a colon (':'). The first component is the key type, described in the table below. The second component is a type-specific payload, using percent-encoding. For all currently supported key types, the decoded form is identical to the contents of the nativeKey() field.

Key typeString representation
PosixRealtime"posix"
SystemV"systemv"
Windows"windows"
Non-standard SystemV"systemv-" followed by a decimal number

This format resembles a URI and allows parsing using URI/URL-parsing functions, such as QUrl. When parsed by such API, the key type will show up as the scheme, and the payload will be the path. Use of query or fragments is reserved.

See also QSharedMemory and QSystemSemaphore.

Member Type Documentation

enum class QNativeIpcKey::Type

This enum describes the backend type for the IPC object. For details on the key types, see the Native IPC Keys documentation.

ConstantValueDescription
QNativeIpcKey::Type::SystemV0x51X/Open System Initiative (XSI) or System V (SVr4) API
QNativeIpcKey::Type::PosixRealtime0x100IEEE 1003.1b (POSIX.1b) API
QNativeIpcKey::Type::Windows0x101Win32 API

See also setType() and type().

Member Function Documentation

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

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

Constructs a QNativeIpcKey object holding native key key (or empty on the overload without the parameter) for type type.

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

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

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

QNativeIpcKey::QNativeIpcKey(const QNativeIpcKey &other)

Copies or moves the content of other.

[constexpr noexcept] QNativeIpcKey::QNativeIpcKey()

Constructs a QNativeIpcKey object of type DefaultTypeForOs with an empty key.

[noexcept] QNativeIpcKey::~QNativeIpcKey()

Disposes of this QNativeIpcKey object.

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

Parses the string form text and returns the corresponding QNativeIpcKey. String representations are useful to inform other processes of the key this process created and they should attach to.

If the string could not be parsed, this function returns an invalid object.

See also toString() and isValid().

[noexcept] bool QNativeIpcKey::isEmpty() const

Returns true if the nativeKey() is empty.

See also nativeKey().

[noexcept] bool QNativeIpcKey::isValid() const

Returns true if this object contains a valid native IPC key type. Invalid types are usually the result of a failure to parse a string representation using fromString().

This function performs no check on the whether the key string is actually supported or valid for the current operating system.

See also type() and fromString().

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

Returns the Type that corresponds to the native IPC key that QSharedMemory and QSystemSemaphore used to use prior to Qt 6.6. Applications and libraries that must retain compatibility with code using either class that was compiled with Qt prior to version 6.6 can use this function to determine what IPC type the other applications may be using.

Note that this function relies on Qt having been built with identical configure-time options.

[noexcept] QString QNativeIpcKey::nativeKey() const

Returns the native key string associated with this object.

See also setNativeKey() and type().

void QNativeIpcKey::setNativeKey(const QString &newKey)

Sets the native key for this object to newKey.

See also nativeKey() and setType().

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

Sets the IPC type of this object to type.

See also type() and setNativeKey().

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

Swaps the native IPC key and type other with this object. This operation is very fast and never fails.

QString QNativeIpcKey::toString() const

Returns the string representation of this object. String representations are useful to inform other processes of the key this process created and that they should attach to.

This function returns a null string if the current object is invalid.

See also fromString().

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

Returns the key type associated with this object.

See also nativeKey() and setType().

Member Variable Documentation

const QNativeIpcKey::Type QNativeIpcKey::DefaultTypeForOs

This constant expression variable holds the default native IPC type for the current OS. It will be Type::Windows for Windows systems and Type::PosixRealtime elsewhere. Note that this constant is different from what QSharedMemory and QSystemSemaphore defaulted to on the majority of Unix systems prior to Qt 6.6; see legacyDefaultTypeForOs() for more information.

Related Non-Members

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

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

Returns true if the lhs and rhs objects hold the same (or different) contents.

[noexcept] size_t qHash(const QNativeIpcKey &ipcKey)

Returns the hash value for ipcKey, using a default seed of 0.

[noexcept] size_t qHash(const QNativeIpcKey &ipcKey, size_t seed)

Returns the hash value for ipcKey, using seed to seed the calculation.

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

Swaps the native IPC key and type value1 with value2. This operation is very fast and never fails.

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