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

    }

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

相关推荐
yugi98783829 分钟前
基于Qt框架开发多功能视频播放器
开发语言·qt
MeowKnight9589 小时前
【Qt】Qt实践记录2——TCP通信服务器和客户端demo
笔记·qt
Larry_Yanan16 小时前
QML学习笔记(五十二)QML与C++交互:数据转换——时间和日期
开发语言·c++·笔记·qt·学习·ui·交互
十启树20 小时前
Qt 中实现炫酷的开机启动动画
qt
一叶之秋14122 天前
QT背景介绍与环境搭建
开发语言·qt
QT 小鲜肉2 天前
【QT/C++】Qt网络编程进阶:UDP通信和HTTP请求的基本原理和实际应用(超详细)
c语言·网络·c++·笔记·qt·http·udp
四维碎片2 天前
【Qt】大数据量表格刷新优化--只刷新可见区域
开发语言·qt
一叶之秋14122 天前
Qt开发初识
开发语言·qt
梵尔纳多2 天前
ffmpeg 使用滤镜实现播放倍速
c++·qt·ffmpeg
QT 小鲜肉2 天前
【QT/C++】Qt网络编程进阶:TCP网络编程的基本原理和实际应用(超详细)
c语言·开发语言·网络·c++·qt·学习·tcp/ip