qt QTextList详解

1. 概述

QTextList是Qt框架中用于表示文本列表的类。它允许在QTextDocument或QTextBlock中创建和管理有序列表(如编号列表)和无序列表(如符号列表)。QTextList与QTextCursor结合使用时,可以方便地在富文本编辑器中插入、编辑和删除列表项。

2. 重要方法

QTextList类的重要方法包括:

  • 构造函数:QTextList(QTextDocument *doc) - 创建一个与指定QTextDocument关联的QTextList对象。
  • addItem():void addItem(const QTextBlock &block) - 将一个已存在的QTextBlock添加到列表中作为新项。
  • removeItem():void removeItem(int index) - 从列表中移除指定索引处的项。
  • item():QTextBlock item(int index) const - 返回列表中指定索引处的项(QTextBlock对象)。
  • count():int count() const - 返回列表中的项数。
  • setFormat():void setFormat(const QTextBlockFormat &format) - 设置列表中所有项的格式。
  • format():QTextBlockFormat format(int index = -1) const - 返回列表中指定索引处项的格式,如果索引为-1,则返回列表的格式(通常用于新添加的项)。
  • setIndent():void setIndent(qreal indent) - 设置列表项的缩进量。
  • indent():qreal indent() const - 返回列表项的缩进量。
3、常用枚举类型

以下是 QTextList 类中一些常用的枚举类型及其简要介绍:

  • QTextListFormat::style:列表样式。

  • QTextListFormat::ListDisc:无序列表,项目前使用实心圆点。

  • QTextListFormat::ListCircle:无序列表,项目前使用空心圆点。

  • QTextListFormat::ListSquare:无序列表,项目前使用方块。

  • QTextListFormat::ListDecimal:有序列表,项目前使用数字。

  • QTextListFormat::ListLowerAlpha:有序列表,项目前使用小写字母。

  • QTextListFormat::ListUpperAlpha:有序列表,项目前使用大写字母。

  • QTextListFormat::ListLowerRoman:有序列表,项目前使用小写罗马数字。

  • QTextList::ListUpperRoman:有序列表,项目前使用大写罗马数字。

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
    setWindowTitle("QTextList Example");
    resize(400, 300);

    复制代码
      // 创建UI组件
      textEdit = new QTextEdit();
      createListButton = new QPushButton("Create List");
      addItemButton = new QPushButton("Add Item");
      removeItemButton = new QPushButton("Remove Item");
    
      // 布局管理
      QVBoxLayout *layout = new QVBoxLayout;
      layout->addWidget(textEdit);
      layout->addWidget(createListButton);
      layout->addWidget(addItemButton);
      layout->addWidget(removeItemButton);
      QWidget *centralWidget = new QWidget;
      centralWidget->setLayout(layout);
      setCentralWidget(centralWidget);
    
      // 连接信号和槽
      connect(createListButton, &QPushButton::clicked, this, &MainWindow::createTextList);
      connect(addItemButton, &QPushButton::clicked, this, &MainWindow::addListItem);
      connect(removeItemButton, &QPushButton::clicked, this, &MainWindow::removeListItem);

    }

    MainWindow::~MainWindow()
    {
    }

    void MainWindow::createTextList()
    {
    QTextDocument *doc = textEdit->document();
    QTextCursor cursor(doc);

    复制代码
      QTextListFormat listFormat;
      listFormat.setStyle(QTextListFormat::ListDisc);
      cursor.createList(listFormat);
    
      cursor.insertBlock();
      cursor.insertText("Item 1");
    
      cursor.insertBlock();
      cursor.insertText("Item 2");
    
      cursor.insertBlock();
      cursor.insertText("Item 3");

    }

    void MainWindow::addListItem()
    {
    QTextDocument *doc = textEdit->document();
    QTextCursor cursor(doc);

    复制代码
      QTextList *list = cursor.currentList();
      if (list) {
          cursor.insertBlock();
          cursor.insertText("New Item");
          list->add(cursor.block());
      } else {
          qDebug() << "No list found to add item.";
      }

    }

    void MainWindow::removeListItem()
    {
    QTextDocument *doc = textEdit->document();
    QTextCursor cursor(doc);

    复制代码
      QTextList *list = cursor.currentList();
      if (list && list->count() > 0) {
          list->removeItem(list->count() - 1);
      } else {
          qDebug() << "No list found or list is empty.";
      }

    }

觉得有帮助的话,打赏一下呗。。

相关推荐
Smile丶凉轩4 小时前
Qt 界面优化(绘图)
开发语言·数据库·c++·qt
charlie11451419110 小时前
基于Qt6 + MuPDF在 Arm IMX6ULL运行的PDF浏览器——MuPDF Adapter文档
arm开发·qt·学习·pdf·教程·设计·qt6
电信2301杨臣11 小时前
QT---信号与槽
开发语言·qt
范纹杉想快点毕业14 小时前
以项目的方式学QT开发(一)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·数据结构·c++·git·qt·链表·github
钢铁男儿15 小时前
PyQt 探索QMainWindow:打造专业的PyQt5主窗
python·qt·pyqt
破晓的历程19 小时前
Qt file文件操作详解
开发语言·qt
躺着听Jay21 小时前
QT设置MySQL驱动
数据库·qt
范纹杉想快点毕业1 天前
以项目的方式学QT开发(三)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·开发语言·c++·qt·mysql·算法·命令模式
机器视觉知识推荐、就业指导1 天前
Qt/C++面试【速通笔记九】—视图框架机制
c++·笔记·qt