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.";
      }
    

    }

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

相关推荐
cpp_learners4 小时前
QT 引入Quazip和Zlib源码工程到项目中,无需编译成库,跨平台,压缩进度
qt·zlib·加密压缩·quazip
数巨小码人8 小时前
QT SQL框架及QSqlDatabase类
jvm·sql·qt
程序员老舅11 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
enyp8012 小时前
Qt QStackedWidget 总结
开发语言·qt
luoyayun36112 小时前
Trae+Qt+MSVC环境配置
vscode·qt·环境配置·trae qt
水瓶丫头站住20 小时前
Qt中QDockWidget的使用方式
开发语言·qt
laimaxgg20 小时前
Qt常用控件之数字显示控件QLCDNumber
开发语言·c++·qt·qt5·qt6.3
牵牛老人21 小时前
Qt开发中出现中文乱码问题深度解析与解决方案
开发语言·qt
Zfox_1 天前
【QT】信号与槽 & 窗口坐标
开发语言·c++·qt·qt5
进击ing小白1 天前
Qt程序退出相关资源释放问题
开发语言·qt