感谢如是不二
- 文本块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太多功能了...
参考: