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

    }

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

相关推荐
feiyangqingyun7 小时前
有难度哦/Qt基于通用地图组件实现航迹规划和模拟/动态标注轨迹线/带序号和方向箭头指示
qt·航迹规划和模拟
江公望7 小时前
Qt qmlRegisterSingletonType()函数浅谈
c++·qt
HyEISN7 小时前
关于 Qt 6.10.0 中 FolderListModel 返回 undefined 路径
qt
江公望9 小时前
Qt QtConcurrent使用入门浅解
c++·qt·qml
Larry_Yanan14 小时前
QML学习笔记(三十一)QML的Flow定位器
java·前端·javascript·笔记·qt·学习·ui
Larry_Yanan14 小时前
QML学习笔记(三十)QML的布局器(Layouts)
c++·笔记·qt·学习·ui
feiyangqingyun14 小时前
Qt编写上下界面切换效果/前进到下一个界面/后退到上一个页面/零件工艺及管理设计系统
qt·零件工艺
ajassi200015 小时前
开源 C++ QT QML 开发(十五)通讯--http下载
c++·qt·开源
枫叶丹417 小时前
【Qt开发】输入类控件(六)-> QDial
开发语言·qt
咯哦哦哦哦19 小时前
关于QT 打印中文 乱码问题
java·数据库·qt