Qt::Literals::StringLiterals Namespace

namespace Qt::Literals::StringLiterals

StringLiterals 命名空间为 Qt 类型声明了字符串字面运算符。更多

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

功能

(since 6.4) QLatin1Char operator""_L1(char ch)
(since 6.4) QLatin1StringView operator""_L1(const char *str, size_t size)
(since 6.4) QByteArray operator""_ba(const char *str, size_t size)
(since 6.4) QString operator""_s(const char16_t *str, size_t size)

详细说明

内联Qt::Literals::StringLiterals 命名空间声明了 Qt 类型的字符串字面操作符。由于LiteralsStringLiterals 命名空间都声明为内联,因此可通过在代码中添加以下符号之一来访问该命名空间中的符号:

// Makes visible only the literal operators declared in StringLiterals
using namespace Qt::Literals::StringLiterals;

// Makes visible literal operators declared in all inline namespaces
// inside Literals
using namespace Qt::Literals;

// Makes visible all symbols (including all literal operators) declared
// in the Qt namespace
using namespace Qt;

函数文档

[constexpr noexcept, since 6.4] QLatin1Char operator""_L1(char ch)

字面运算符,从ch 创建QLatin1Char

下面的代码创建了一个QLatin1Char

using namespace Qt::Literals::StringLiterals;

auto ch = 'a'_L1;

此函数在 Qt 6.4 中引入。

另请参阅 Qt::Literals::StringLiterals

[constexpr noexcept, since 6.4] QLatin1StringView operator""_L1(const char *str, size_t size)

字面运算符,用字符串字面str 中的第一个size 字符创建QLatin1StringView

下面的代码创建了一个QLatin1StringView

using namespace Qt::Literals::StringLiterals;

auto str = "hello"_L1;

该函数在 Qt 6.4 中引入。

另请参阅 Qt::Literals::StringLiterals

[noexcept, since 6.4] QByteArray operator""_ba(const char *str, size_t size)

字面运算符,用字符串字面str 中的第一个size 字符创建QByteArray

QByteArray 在编译时创建,生成的字符串数据存储在编译对象文件的只读段中。重复的字面量可以共享同一个只读存储器。该功能可与QByteArrayLiteral 互换使用,但当代码中有许多字符串字面量时,可节省键入次数。

以下代码创建了一个QByteArray

using namespace Qt::Literals::StringLiterals;

auto str = "hello"_ba;

此功能在 Qt 6.4 中引入。

另请参见 Qt::Literals::StringLiterals

[noexcept, since 6.4] QString operator""_s(const char16_t *str, size_t size)

字面运算符,用 char16_t 字符串字面str 中的第一个size 字符创建QString

QString 在编译时创建,生成的字符串数据存储在编译对象文件的只读段中。重复的字面量可以共享同一个只读存储器。该功能可与QStringLiteral 互换使用,但当代码中有许多字符串字面量时,可节省键入次数。

以下代码创建了一个QString

using namespace Qt::Literals::StringLiterals;

auto str = u"hello"_s;

此功能在 Qt 6.4 中引入。

另请参见 Qt::Literals::StringLiterals

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