富文本(文本块)

感谢如是不二

  • 文本块QTextBlock类为文本文档QTextDocument提供了一个文本片段QTextFragment的容器。
  • 一个文本块可以理解为一个段落。
  • 但是,不要用回车换行!!!
  • 因为回车换行,表示再新建一个文本块。
  • QTextBlock提供了只读接口,是文档层次接口的一部分

???

  • QTextBlock格式设置由QTextBlockFormat类来负责,可以进行对齐方式、四周缩进等。
  • QTextBlock的文本内容格式由QTextCharFormat类来负责,比如字体大小、加粗、下划线等。

上点代码:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextFrame>
#include <QDebug>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    void showTextFrame();
};

#endif // MAINWINDOW_H

 

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // get Document Obj
    QTextDocument *document = ui->textEdit->document();

    // get root frame
    QTextFrame *rootFrame = document->rootFrame();

    // create Formate of rootFrame
    QTextFrameFormat format;
    format.setBorderBrush(Qt::blue);
    format.setBorder(2);
    rootFrame->setFrameFormat(format);

    // create a subFrame in rootFrame
    QTextFrameFormat sub_format;
    sub_format.setBackground(Qt::gray);
    sub_format.setMargin(8); // outside distance
    sub_format.setPadding(10); // inside distance
    sub_format.setBorder(3); // border width
    sub_format.setBorderStyle(QTextFrameFormat::BorderStyle_Dashed);

    // insert formate in cursor's position
    QTextCursor cursor = ui->textEdit->textCursor();
    cursor.insertFrame(sub_format);

    // ----
    QAction *action = new QAction(tr("showTextFrame"),this);
    connect(action,&QAction::triggered,this,&MainWindow::showTextFrame);
    ui->mainToolBar->addAction(action);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showTextFrame()
{
    // get document
    QTextDocument *document = ui->textEdit->document();

    // get root Frame
    QTextFrame *rootFrame = document->rootFrame();

    //
    QTextFrame::iterator it;
    for(it = rootFrame->begin();!(it.atEnd());++it)
    {
        // get current text pointer
        QTextFrame *childFrame = it.currentFrame();

        // get current text block
        QTextBlock childBlock = it.currentBlock();

        if(childFrame)
        {
            qDebug() << "Frame";
        }
        if(childBlock.isValid())
        {
            qDebug() << "Block: " << childBlock.text();
        }
    }

}

主要代码是showTextFrame!

????

好像有一个文本块没有被输出,就是那个位于子frame中的一个文本块!

解惑:

void MainWindow::showTextBlock()
{
    QTextDocument *document = ui->textEdit->document();
    // QTextFrame *rootFrame = document->rootFrame();
    QTextBlock block = document->firstBlock();

    for(int i = 0; i < document->blockCount(); i++)
    {
        qDebug() << tr("文本块:%1,文本块首行行号:%2,文本块长度:%3,内容为:").
                    arg(i).arg(block.firstLineNumber()).arg(block.length())<<
                    block.text();
        block = block.next();
    }
}

重点在于 document.firstBlock() 函数才能遍历所有的 TextBlock!

  • 注意文本块长度,如果你一个文本块什么都没有输入,他的长度还是1而不是0!
  • 行号是从0开始计的!

TextcharFormat

    // 测试TextCharFormat
    QTextCharFormat charFormat;
    charFormat.setBackground(Qt::blue);
    charFormat.setForeground(Qt::red);
    charFormat.setFont(QFont(tr("微软雅黑"),18,QFont::Bold));
    cursor.setCharFormat(charFormat);
    cursor.insertText("我喜欢吃水果!");

TextEdit太多功能了...

 

 

参考:

不二如是:https://fishc.com.cn/forum.php?mod=viewthread&tid=77036&extra=page%3D5%26filter%3Dauthor%26orderby%3Ddateline%26typeid%3D449

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柔弱胜刚强.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值