本页内容

QTextList Class

QTextList 类提供了一个以QTextDocument 为背景的、经过装饰的项目列表。更多内容...

头文件: #include <QTextList>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承自: QTextBlockGroup

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

公共函数

void add(const QTextBlock &block)
int count() const
QTextListFormat format() const
QTextBlock item(int i) const
int itemNumber(const QTextBlock &block) const
QString itemText(const QTextBlock &block) const
void remove(const QTextBlock &block)
void removeItem(int i)
void setFormat(const QTextListFormat &format)

详细说明

列表包含一系列文本块,每个文本块都标有项目符号或其他符号。可以使用多级列表,自动编号功能支持有序的数字和字母列表。

列表是通过使用文本光标在当前位置插入一个空列表,或者将现有文本移入一个新列表来创建的。QTextCursor::insertList() 函数在文档的光标位置插入一个空块,并将其作为列表中的第一个项目。

    QTextListFormat listFormat;
    if (list) {
        listFormat = list->format();
        listFormat.setIndent(listFormat.indent() + 1);
    }

    listFormat.setStyle(QTextListFormat::ListDisc);
    cursor.insertList(listFormat);

QTextCursor::createList() 函数将光标当前块中的内容转换为新列表的第一个项目。

可通过QTextCursor::currentList() 获取光标当前所在的列表。

列表中的项目数量可通过count()获取。每个项目均可通过item()函数根据其在列表中的索引获取。同样,特定项目的索引可通过itemNumber()获取。每个项目的文本内容可通过itemText()函数获取。

请注意,列表中的项目在文档中可能并非相邻元素。例如,多级列表中的顶级项目会与列表中较低级别的项目分隔开来。

可通过removeItem()函数按索引删除列表项。remove()函数用于删除列表中的指定项。

列表的格式通过setFormat()设置,并通过format()读取。该格式描述的是列表本身的样式,而非单个项目。

另请参阅 QTextBlock 、QTextListFormat 和QTextCursor 。

成员函数文档

void QTextList::add(const QTextBlock &block)

将给定的block 作为列表的一部分。

另请参阅 remove() 和removeItem()。

int QTextList::count() const

返回列表中的项目数量。

QTextListFormat QTextList::format() const

返回列表的格式。

另请参阅 setFormat()。

QTextBlock QTextList::item(int i) const

返回列表中第i 个文本块。

另请参阅 count() 和itemText()。

int QTextList::itemNumber(const QTextBlock &block) const

返回与给定的block 相对应的列表项的索引。如果列表中不存在该块,则返回-1。

QString QTextList::itemText(const QTextBlock &block) const

返回与给定的block 相对应的列表项的文本。

void QTextList::remove(const QTextBlock &block)

从列表中移除给定的block 。

另请参阅 add() 和removeItem()。

void QTextList::removeItem(int i)

从列表中移除位于项位置 `i ` 的项。当列表中的最后一项被移除时,拥有该列表的 `QTextDocument ` 会自动删除该列表。

另请参阅 add() 和remove()。

void QTextList::setFormat(const QTextListFormat &format)

将列表的格式设置为format 。

另请参阅 format()。

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