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

    }

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

相关推荐
uyeonashi3 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
galaxy_strive13 小时前
绘制饼图详细过程
开发语言·c++·qt
委婉待续1 天前
Qt的学习(一)
开发语言·qt·学习
笨笨马甲1 天前
Qt Quick Layout功能及架构
开发语言·qt
feiyangqingyun1 天前
Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动
c++·qt·udp·gb28181
jllws11 天前
Qt学习及使用_第1部分_认识Qt---学习目的及技术准备
qt·c++框架
到点就困告1 天前
海康工业相机SDK二次开发(VS+QT+海康SDK+C++)
数码相机·qt·海康
唐墨1232 天前
android与Qt类比
android·开发语言·qt
道剑剑非道2 天前
QT开发技术【ffmpeg + QAudioOutput】音乐播放器 完善
开发语言·qt·ffmpeg
不惑_2 天前
用 PyQt5 打造一个可视化 JSON 数据解析工具
开发语言·qt·json