Qt::Literals::StringLiterals Namespace
namespace Qt::Literals::StringLiteralsStringLiterals 命名空间声明了 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 字符串类型的字面量运算符。由于Literals 和StringLiterals 这两个命名空间均被声明为内联,因此可以通过在代码中添加以下任一方式来访问该命名空间中的符号:
// Makes visible only the literal operators declared in StringLiterals
using namespace Qt::StringLiterals;
// same, but needlessly verbose:
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;不建议使用后者,因为如果 Qt 在Qt::Literals 内的不同命名空间中添加了冲突的操作符,这种写法将无法让你选择所需的字面量操作符。
函数文档
[constexpr noexcept, since 6.4] QLatin1Char operator""_L1(char ch)
一个字面量运算符,用于将ch 转换为QLatin1Char 。
以下代码创建了一个QLatin1Char :
using namespace Qt::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::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::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)
一个字面量运算符,它从字符串字面量str 的前size 个字符中生成一个QString 。
该QString 在编译时创建,生成的字符串数据存储在编译后对象文件的只读段中。重复的字符串字面量可能共享同一块只读内存。此功能与QStringLiteral 互换,但在代码中存在大量字符串字面量时可节省输入工作量。
以下代码创建了一个QString :
using namespace Qt::StringLiterals;
auto str = u"hello"_s;该函数于 Qt 6.4 中引入。
另请参阅 Qt::Literals::StringLiterals 。
© 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.