本页内容

QUtf8StringView Class

QUtf8StringView 类通过QString API 的只读子集,为 UTF-8 字符串提供统一的视图。更多内容...

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

注意:该类中的所有函数均为可重入函数。

QUtf8StringView 比较

类别可比较类型描述
strongQUtf8StringView
strongchar16_t、QChar 、const char16_t *、QString 、QStringView 以及QLatin1StringView 。
strongconst char *、QByteArray 和QByteArrayView 。字节数组的内容被解释为 UTF-8。

公共类型

公共函数

QUtf8StringView()
QUtf8StringView(const Char (&string)[N])
QUtf8StringView(const Char *str)
QUtf8StringView(const Container &str)
QUtf8StringView(std::nullptr_t)
QUtf8StringView(const Char *first, const Char *last)
QUtf8StringView(const Char *str, qsizetype len)
(since 6.9) QString arg(Args &&... args) const
QUtf8StringView::storage_type at(qsizetype n) const
QUtf8StringView::storage_type back() const
QUtf8StringView::const_iterator begin() const
QUtf8StringView::const_iterator cbegin() const
QUtf8StringView::const_iterator cend() const
void chop(qsizetype n)
QUtf8StringView chopped(qsizetype n) const
(since 6.5) int compare(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.5) int compare(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.5) int compare(QUtf8StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QUtf8StringView::const_reverse_iterator crbegin() const
QUtf8StringView::const_reverse_iterator crend() const
QUtf8StringView::const_pointer data() const
bool empty() const
QUtf8StringView::const_iterator end() const
QUtf8StringView first(qsizetype n) const
QUtf8StringView::storage_type front() const
bool isEmpty() const
bool isNull() const
(since 6.3) bool isValidUtf8() const
QUtf8StringView last(qsizetype n) const
qsizetype length() const
(since 6.8) qsizetype max_size() const
QUtf8StringView::const_reverse_iterator rbegin() const
QUtf8StringView::const_reverse_iterator rend() const
qsizetype size() const
(since 6.8) QUtf8StringView &slice(qsizetype pos, qsizetype n)
(since 6.8) QUtf8StringView &slice(qsizetype pos)
QUtf8StringView sliced(qsizetype pos) const
QUtf8StringView sliced(qsizetype pos, qsizetype n) const
QString toString() const
void truncate(qsizetype n)
const char8_t *utf8() const
(since 6.7) operator std::string_view() const
(since 6.10) operator std::u8string_view() const
QUtf8StringView::storage_type operator[](qsizetype n) const

静态公共成员

QUtf8StringView fromArray(const Char (&string)[Size])
(since 6.8) qsizetype maxSize()

详细描述

QUtf8StringView 引用其并不拥有的 UTF-8 字符串中的一段连续部分。它作为各种 UTF-8 字符串的接口类型,无需先构建QString 或QByteArray 。

该 UTF-8 字符串可以表示为char8_t 、char 、signed char 或unsigned char 的数组(或与数组兼容的数据结构,如 std::basic_string 等)。

QUtf8StringView 被设计为一种接口类型;其主要用例是作为函数参数类型。 当 QUtf8StringView 用作自动变量或数据成员时,必须注意确保所引用的字符串数据(例如,由 std::u8string 拥有的数据)在所有代码路径中都比 QUtf8StringView 存活更久,以免字符串视图最终引用已被删除的数据。

当用作接口类型时,QUtf8StringView 允许单个函数接受各种各样的 UTF-8 字符串数据源。 因此,一个接受 QUtf8StringView 的函数既可以取代多个函数重载(例如接受 `QByteArray`),同时还能支持将更多类型的字符串数据源传递给该函数,例如 `u8"Hello World"`、char8_t (C++20)或 `char `(C++17)字符串字面量。 使用 QUtf8StringView 时,C++17 与 C++20 之间在char8_t 方面的兼容性问题将不复存在。

与所有视图一样,QUtf8StringView 应按值传递,而非按常量引用传递:

    void myfun1(QUtf8StringView sv);        // preferred
    void myfun2(const QUtf8StringView &sv); // compiles and works, but slower

如果您希望让用户在向函数传递字符串时拥有最大的自由度,请考虑改用QAnyStringView 。

QUtf8StringView 也可用作函数的返回值。 如果调用一个返回 QUtf8StringView 的函数,请务必注意:QUtf8StringView 的保留时间不应超过该函数承诺保留所引用字符串数据的时间。如有疑问,请通过调用toString() 将 QUtf8StringView 转换为QString ,从而获得对数据的强引用。

QUtf8StringView 是一种字面量类型。

兼容的字符类型

QUtf8StringView 支持多种字符类型的字符串:

  • char (包括有符号和无符号类型)
  • char8_t (仅限 C++20)

大小和子字符串

QUtf8StringView 函数中的所有大小和位置均以 UTF-8 码点为单位(即,UTF-8 多字节序列根据其长度计为两个、三个或四个码点)。 QUtf8StringView 不会尝试检测或阻止直接切片穿过 UTF-8 多字节序列。这与QStringView 及代理对的情况类似。

C++20、char8_t 和 QUtf8StringView

在 C++20 中,u8"" 的字符串字面量类型从 `const char[] ` 更改为 `const char8_t[]`。如果 Qt 6 能依赖 C++20,QUtf8StringView 将原生存储 `char8_t `,并且以下函数和别名将使用(指向)`char8_t`:

这正是 QUtf8StringView 在 Qt 7 中应有的样子,但在 Qt 6 中尚无法实现。为了避免将用户锁定在 C++17 时代的接口中长达十年之久,Qt 提供了两个位于不同(内联)命名空间中的 QUtf8StringView 类。 第一个位于q_no_char8_t 命名空间中,其value_type 为const char ,可普遍使用。第二个位于q_has_char8_t 命名空间中,其value_type 为const char8_t ,仅在 C++20 模式下编译时可用。

q_no_char8_t 这是一个内联命名空间,与 C++ 版本无关,以避免意外的二进制不兼容。若要使用char8_t 版本,需通过q_has_char8_t::QUtf8StringView 显式指定其名称。

在内部,二者都是同一模板类 QBasicUtf8StringView 的实例化。请勿在源代码中使用该模板类的名称。

另请参阅 QAnyStringView 、QStringView 、QLatin1StringView 以及QString 。

成员类型文档

QUtf8StringView::const_iterator

此 typedef 为 `QUtf8StringView` 提供了一个 STL 风格的 const 迭代器。

另请参阅 iterator 和const_reverse_iterator 。

QUtf8StringView::const_pointer

value_type * 的别名。为兼容 STL 而提供。

QUtf8StringView::const_reference

value_type & 的别名。为兼容 STL 而提供。

QUtf8StringView::const_reverse_iterator

此 typedef 为 `QUtf8StringView` 提供了一个 STL 风格的 const 反向迭代器。

另请参阅 reverse_iterator 和const_iterator 。

QUtf8StringView::difference_type

std::ptrdiff_t 的别名。为兼容 STL 而提供。

QUtf8StringView::iterator

此 typedef 为QUtf8StringView 提供了一个 STL 风格的 const 迭代器。

QUtf8StringView 由于不支持可变迭代器,因此它与 `const_iterator` 完全相同。

另请参阅 const_iterator 和reverse_iterator 。

QUtf8StringView::pointer

value_type * 的别名。为兼容 STL 而提供。

QUtf8StringView 不支持可变指针,因此该函数与 `const_pointer` 效果相同。

QUtf8StringView::reference

value_type & 的别名。为兼容 STL 而提供。

QUtf8StringView 不支持可变引用,因此这与 `const_reference` 效果相同。

QUtf8StringView::reverse_iterator

此 typedef 为 `QUtf8StringView` 提供了一个 STL 风格的 const 反向迭代器。

QUtf8StringView 由于不支持可变反向迭代器,因此这与 `const_reverse_iterator` 完全相同。

另请参阅 ` const_reverse_iterator ` 和 `iterator`。

QUtf8StringView::size_type

qsizetype 的别名。为与 STL 兼容而提供。

[alias] QUtf8StringView::storage_type

char 的别名。

QUtf8StringView::value_type

const char 的别名。为兼容 STL 而提供。

成员函数文档

[constexpr noexcept] QUtf8StringView::QUtf8StringView()

构建一个空字符串视图。

另请参阅 isNull()。

[constexpr noexcept] template <typename Char, size_t N> QUtf8StringView::QUtf8StringView(const Char (&string)[N])

基于字符串字面量string 构建一个字符串视图。该视图覆盖该数组,直至遇到第一个Char(0) ,或者N ,以先发生者为准。如果需要整个数组,请改用fromArray()。

string 必须在该字符串视图对象的生命周期内始终有效。

约束

仅当string 是实际数组,且Char 是兼容字符类型时,才参与重载解析。兼容字符类型包括:char8_t 、char 、signed char 和unsigned char 。

另请参阅 fromArray()。

[constexpr noexcept] template <typename Char> QUtf8StringView::QUtf8StringView(const Char *str)

在str 上构建一个字符串视图。其长度由扫描找到第一个Char(0) 的字符串长度决定。

str 在该字符串视图对象的生命周期内,该参数必须始终有效。

将nullptr 作为str 传递是安全的,并将生成一个空字符串视图。

约束

仅当str 不是数组,且Char 是兼容的字符类型时,才参与重载解析。兼容的字符类型包括:char8_t 、char 、signed char 和unsigned char 。

[constexpr noexcept] template <typename Container> requires if_compatible_container<Container> QUtf8StringView::QUtf8StringView(const Container &str)

在str 上构建一个字符串视图。长度取自std::size(str) 。

std::data(str) 在该字符串视图对象的生命周期内,该长度必须始终有效。

当且仅当std::size(str) == 0 时,字符串视图为空。该构造函数是否会生成空字符串视图未作规定(若要实现这一点,std::data(str) 必须返回nullptr )。

约束

仅当Container 是与value_type 具有兼容字符类型的容器时,才参与重载解析。兼容的字符类型包括:char8_t 、char 、signed char 和unsigned char 。

另请参阅 isNull() 和isEmpty()。

[constexpr noexcept] QUtf8StringView::QUtf8StringView(std::nullptr_t)

构建一个空字符串视图。

另请参阅 ` isNull()`。

[constexpr] template <typename Char> requires if_compatible_char<Char> QUtf8StringView::QUtf8StringView(const Char *first, const Char *last)

在first 上构建一个字符串视图,其长度为(last -first)。

在该字符串视图对象的生命周期内,[first,last) 范围必须始终有效。

如果last 也是nullptr ,则将\nullptr 作为first 传递是安全的,并将生成一个空字符串视图。

如果last 位于first 之前,或者first 为nullptr 而last 不是,则行为未定义。

约束

仅当Char 是兼容的字符类型时,才参与重载解析。兼容的字符类型包括:char8_t 、char 、signed char 和unsigned char 。

[constexpr] template <typename Char> requires if_compatible_char<Char> QUtf8StringView::QUtf8StringView(const Char *str, qsizetype len)

在str 上构建一个长度为len 的字符串视图。

在该字符串视图对象的生命周期内,[str,len) 范围必须始终有效。

如果len 也是 0,则将nullptr 作为str 传递是安全的,并将生成一个空字符串视图。

如果len 为负数,或者当其为正数时str 等于nullptr ,则行为未定义。

约束

仅当Char 是兼容的字符类型时,才参与重载解析。兼容的字符类型包括:char8_t 、char 、signed char 和unsigned char 。

[since 6.9] template <typename... Args> QString QUtf8StringView::arg(Args &&... args) const

将该字符串中所有%N 替换为args 中的相应参数。这些参数不按位置对应:args 中的第一个参数将%N 替换为最低的N (所有该值),args 中的第二个参数将%N 替换为次低的N ,以此类推。

Args 可以包含任何能隐式转换为QAnyStringView 的类型。

该函数在 Qt 6.9 中引入。

另请参阅 QString::arg(Args&&...)。

[constexpr] QUtf8StringView::storage_type QUtf8StringView::at(qsizetype n) const

返回该字符串视图中位于位置n 处的码点。

如果n 为负数或不小于size(),则行为未定义。

另请参阅 operator[]()、front() 和back()。

[constexpr] QUtf8StringView::storage_type QUtf8StringView::back() const

返回字符串视图中的最后一个码点。与last() 相同。

提供此函数是为了兼容 STL。

警告: 对空字符串视图调用 此函数将导致未定义行为。

另请参阅 front()。

[noexcept] QUtf8StringView::const_iterator QUtf8StringView::begin() const

返回一个指向字符串视图中第一个码点的 constSTL 风格迭代器。

提供此函数是为了兼容 STL。

另请参阅 end()、cbegin()、rbegin() 以及data()。

[noexcept] QUtf8StringView::const_iterator QUtf8StringView::cbegin() const

与begin() 相同。

提供此函数是为了兼容 STL。

另请参阅 cend()、begin()、crbegin() 和data()。

[noexcept] QUtf8StringView::const_iterator QUtf8StringView::cend() const

与 `end()` 相同。

提供此函数是为了兼容 STL。

另请参阅 cbegin()、end() 和crend()。

[constexpr] void QUtf8StringView::chop(qsizetype n)

将此字符串视图截断n 个码点。

与 `*this = first(size() - n)` 相同。

注意: 当n < 0 或n >size() 时,行为未定义。

另请参阅 sliced()、first()、last()、chopped() 和truncate()。

[constexpr] QUtf8StringView QUtf8StringView::chopped(qsizetype n) const

返回从该对象开头开始,长度为 `size()` - `n ` 的子字符串。

与first(size() - n) 相同。

注意: 当n < 0 或n >size() 时,行为未定义。

另请参阅 sliced()、first()、last()、chop()、truncate() 以及slice()。

[noexcept, since 6.5] int QUtf8StringView::compare(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[noexcept, since 6.5] int QUtf8StringView::compare(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[noexcept, since 6.5] int QUtf8StringView::compare(QUtf8StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

将此字符串视图与str 进行比较:若此字符串视图小于str ,则返回负整数;若大于str ,则返回正整数;若两者相等,则返回零。

如果 `cs ` 为 `Qt::CaseSensitive `(默认值),则比较区分大小写;否则,比较不区分大小写。

这些函数首次引入于 Qt 6.5。

[noexcept] QUtf8StringView::const_reverse_iterator QUtf8StringView::crbegin() const

与rbegin() 相同。

提供此函数是为了兼容 STL。

另请参见 crend()、rbegin() 和cbegin()。

[noexcept] QUtf8StringView::const_reverse_iterator QUtf8StringView::crend() const

与rend() 相同。

提供此函数是为了与 STL 兼容。

另请参阅 crbegin()、rend() 和cend()。

[constexpr noexcept] QUtf8StringView::const_pointer QUtf8StringView::data() const

返回指向字符串视图中第一个码点的 const 指针。

注意: 返回值所表示的字符数组并非以空字符结尾。

另请参阅 begin()、end() 和utf8()。

[constexpr noexcept] bool QUtf8StringView::empty() const

返回该字符串视图是否为空——即判断size() == 0 是否为真。

提供此函数是为了兼容 STL。

另请参阅 isEmpty()、isNull()、size() 以及length()。

[noexcept] QUtf8StringView::const_iterator QUtf8StringView::end() const

返回一个指向列表中最后一个码点之后那个虚构码点的、符合 STL 风格的const迭代器。

提供此函数是为了兼容 STL。

另请参阅 begin()、cend() 和rend()。

[constexpr] QUtf8StringView QUtf8StringView::first(qsizetype n) const

返回一个字符串视图,其中包含该字符串视图的前n 个代码点。

注意: 当n < 0 或n >size() 时,行为未定义。

另请参阅 last()、sliced()、chopped()、chop()、truncate() 以及slice()。

[static constexpr noexcept] template <typename Char, size_t Size> requires if_compatible_char<Char> QUtf8StringView QUtf8StringView::fromArray(const Char (&string)[Size])

基于完整的字符串字面量string (包含Size 个元素,包括任何尾随的Char(0) )构建一个字符串视图。如果不想将空终止符包含在视图中,可以在确定它位于末尾时通过chop()将其移除。 或者,您可以使用接受数组字面量的构造函数重载,该构造函数将创建一个视图,其范围截至数据中的第一个空终止符(但不包括该空终止符)。

string 在该字符串视图对象的生命周期内,该属性必须始终有效。

如果 `Char ` 是兼容的字符类型,则该函数可与任何数组字面量配合使用。兼容的字符类型包括:char8_t 、char 、signed char 和unsigned char 。

[constexpr] QUtf8StringView::storage_type QUtf8StringView::front() const

返回字符串视图中的第一个码点。与first() 相同。

提供此函数是为了兼容 STL。

警告: 对空字符串视图调用 此函数将导致未定义行为。

另请参阅 back()。

[constexpr noexcept] bool QUtf8StringView::isEmpty() const

返回该字符串视图是否为空——即size() == 0 。

提供此函数是为了与其他 Qt 容器保持兼容性。

另请参阅 empty()、isNull()、size() 和length()。

[constexpr noexcept] bool QUtf8StringView::isNull() const

返回该字符串视图是否为空——即data() == nullptr 。

提供此函数是为了与其他 Qt 容器保持兼容性。

另请参见 empty()、isEmpty()、size() 和length()。

[noexcept, since 6.3] bool QUtf8StringView::isValidUtf8() const

如果该字符串包含有效的 UTF-8 编码数据,则返回 `true `;否则返回 `false `。

该函数在 Qt 6.3 中引入。

[constexpr] QUtf8StringView QUtf8StringView::last(qsizetype n) const

返回一个字符串视图,其中包含该字符串视图中最后n 个代码点。

注意: 当n < 0 或n >size() 时,行为未定义。

另请参阅 first()、sliced()、chopped()、chop()、truncate()以及slice()。

[constexpr noexcept] qsizetype QUtf8StringView::length() const

与size() 相同。

提供此函数是为了与其他 Qt 容器保持兼容。

另请参阅 empty()、isEmpty()、isNull() 以及size() 函数。

[static constexpr noexcept, since 6.8] qsizetype QUtf8StringView::maxSize()

它返回视图理论上能够表示的元素最大数量。实际上,该数量可能会小得多,具体取决于系统可用的内存大小。

该函数于 Qt 6.8 版本中引入。

[constexpr noexcept, since 6.8] qsizetype QUtf8StringView::max_size() const

提供此函数是为了与 STL 兼容。

返回maxSize()。

该函数于 Qt 6.8 中引入。

[noexcept] QUtf8StringView::const_reverse_iterator QUtf8StringView::rbegin() const

返回一个指向字符串视图中第一个码点的、按反向顺序排列的 constSTL 风格反向迭代器。

提供此函数是为了与 STL 兼容。

另请参阅 rend()、crbegin() 和begin()。

[noexcept] QUtf8StringView::const_reverse_iterator QUtf8StringView::rend() const

返回一个STL 风格的反向迭代器,该迭代器指向字符串视图中最后一个码点之后的一个位置,且顺序为反向。

提供此函数是为了兼容 STL。

另请参阅 rbegin()、crend() 和end()。

[constexpr noexcept] qsizetype QUtf8StringView::size() const

返回此字符串视图的大小,单位为 UTF-8 码点(也就是说,就本函数而言,多字节序列会被计为多个码点,这与QString 和QStringView 中代理对的处理方式一致)。

另请参阅 empty()、isEmpty()、isNull() 以及length()。

[constexpr, since 6.8] QUtf8StringView &QUtf8StringView::slice(qsizetype pos, qsizetype n)

将此字符串视图修改为从位置pos 开始,并向后延伸n 个码点。

注意: 当pos < 0、n < 0 或pos +n >size() 时,行为 未定义。

该函数在 Qt 6.8 中引入。

另请参阅 sliced()、first()、last()、chopped()、chop() 以及truncate()。

[constexpr, since 6.8] QUtf8StringView &QUtf8StringView::slice(qsizetype pos)

修改此字符串视图,使其从位置pos 开始,并延伸至该位置的末尾。

注意: 当pos < 0 或pos >size() 时,行为未定义。

这是一个重载函数。

该函数在 Qt 6.8 中引入。

另请参阅 sliced()、first()、last()、chopped()、chop() 以及truncate()。

[constexpr] QUtf8StringView QUtf8StringView::sliced(qsizetype pos) const

返回一个字符串视图,该视图从该对象的pos 位置开始,一直延伸到其末尾。

注意: 当pos < 0 或pos >size() 时,行为未定义。

另请参阅 first()、last()、chopped()、chop()、truncate() 以及slice()。

[constexpr] QUtf8StringView QUtf8StringView::sliced(qsizetype pos, qsizetype n) const

返回一个字符串视图,其中包含该字符串视图中从位置pos 开始的n 个码点。

注意: 当pos < 0、n < 0 或pos +n >size() 时,行为 未定义。

另请参阅 first()、last()、chopped()、chop()、truncate() 以及slice()。

QString QUtf8StringView::toString() const

返回此字符串视图数据的深度拷贝,类型为QString 。

当且仅当该字符串视图为空时,返回值才会是空的QString 。

[constexpr] void QUtf8StringView::truncate(qsizetype n)

将此字符串视图截断为n 个码点。

与*this = first(n) 相同。

注意: 当n < 0 或n >size() 时,行为未定义。

另请参见 sliced()、first()、last()、chopped() 以及chop() 函数。

[noexcept] const char8_t *QUtf8StringView::utf8() const

返回指向字符串视图中第一个码点的 const 指针。

结果以const char8_t* 类型返回,因此该函数仅在C++20模式下编译时可用。

注意: 返回值所表示的字符数组并非以空字符结尾。

另请参见 begin()、end() 和data()。

[noexcept, since 6.7] QUtf8StringView::operator std::string_view() const

将此QUtf8StringView 对象转换为std::string_view 对象。返回的视图将具有与该视图相同的数据指针和长度。

该函数自 Qt 6.7 起引入。

[noexcept, since 6.10] QUtf8StringView::operator std::u8string_view() const

将此QUtf8StringView 对象转换为std::u8string_view 对象。返回的视图将具有与该视图相同的数据指针和长度。

此函数仅在 C++20 模式下编译时可用。

该函数在 Qt 6.10 中引入。

[constexpr] QUtf8StringView::storage_type QUtf8StringView::operator[](qsizetype n) const

返回该字符串视图中位置为n 的码点。

如果n 为负数或不小于size(),则行为未定义。

另请参阅 at()、front() 和back()。

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