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-th 文本块。

另请参阅 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() 。

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