富文本
QTextEdit支持富文本处理,即文档中可使用多种格式,如文字、图片、表格等。
PlainText为纯文本。
由此可类比, windows的记事本就是纯文本编辑器,word就是富文本编辑器。
文档的光标主要基于QTextCursor类,文档的框架主要基于QTextDocument类。
一个富文本的文档结构主要分为几种元素:框架(QTextFrameFormat)、文本块(QTextBlock)、表格(QTextTable)、列表(QTxtList)。
每种元素的格式有相应的format类表示:框架格式(QTextDFrameFormat)、文本块格式(QTextBlockFormat)、表格格式(QText)、列表格式(QTextListFormat)。这些格式通常配合QTextCursor类使用。
QTextEdit类就是一个富文本编辑器,在构建QTextEdit类对象时就已经构建了一个QTextDocument类对象和一个QTextCursor类对象。只需调用他们相应的操作即可。
![image-20220129211052245](https://gitee.com/Do2eM0N/blogimg/raw/master/202202031227890.png)
文档边框格式
示例:
![image-20220129211226050](https://gitee.com/Do2eM0N/blogimg/raw/master/202202031227628.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ui->setupUi(this); QTextDocument* document = ui->textEdit->document();
QTextFrame *rootFrame = document->rootFrame();
QTextFrameFormat format; format.setBorderBrush(Qt::red); format.setBorder(3);
rootFrame->setFrameFormat(format); QTextFrameFormat frameFormat; frameFormat.setBackground(Qt::lightGray); frameFormat.setMargin(10); frameFormat.setPadding(5); frameFormat.setBorder(2); frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_DotDash ); QTextCursor cursor = ui->textEdit->textCursor(); cursor.insertFrame(frameFormat);
|
文本个格式、文本块格式、字符格式
![image-20220130182600946](https://gitee.com/Do2eM0N/blogimg/raw/master/202202031227842.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
QAction* action_textFrame = new QAction("框架",this); connect(action_textFrame,&QAction::triggered,this,&MainWindow::showTextFrame); ui->mainToolBar->addAction(action_textFrame);
QAction* action_textBlock = new QAction("文本块",this); connect(action_textBlock,&QAction::triggered,this,&MainWindow::showTextBlock); ui->mainToolBar->addAction(action_textBlock);
QAction* action_textFont = new QAction("字体",this); action_textFont->setCheckable(true); connect(action_textFont,&QAction::triggered,this,&MainWindow::setTextFont); ui->mainToolBar->addAction(action_textFont);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| void MainWindow::showTextFrame() { QTextDocument* document = ui->textEdit->document(); QTextFrame* frame = document->rootFrame(); QTextFrame::iterator it; for(it = frame->begin();!(it.atEnd());it++) { QTextFrame*childFrame = it.currentFrame(); QTextBlock childBlock = it.currentBlock(); if(childFrame) { qDebug()<<"frame"; } else if(childBlock.isValid()) { qDebug()<<"block:"<<childBlock.text(); } }
}
void MainWindow::showTextBlock() { QTextDocument* document = ui->textEdit->document(); QTextBlock block = document->firstBlock(); for(int i =0 ; i < document->blockCount();i++) { qDebug()<<QString("文本块%1,文本块首行行号为:%2,长度:%3,内容%4").arg(i).arg(block.firstLineNumber()).arg(block.length()).arg(block.text()); block = block.next(); }
}
void MainWindow::setTextFont(bool checked) { if(checked) { QTextCursor cursor = ui->textEdit->textCursor(); QTextBlockFormat blockFormat; blockFormat.setAlignment(Qt::AlignCenter); cursor.insertBlock(blockFormat); QTextCharFormat charFormat; charFormat.setBackground(Qt::lightGray); charFormat.setForeground(Qt::blue); charFormat.setFont(QFont(QString("宋体"),12,QFont::Bold,true)); charFormat.setFontUnderline(true); cursor.setCharFormat(charFormat); cursor.insertText("嘻嘻"); } }
|
文档插入表格、列表、图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| void MainWindow::insertTable() { QTextCursor cursor = ui->textEdit->textCursor(); QTextTableFormat format; format.setCellSpacing(2); format.setCellPadding(10); cursor.insertTable(3, 3,format); }
void MainWindow::insertList() { QTextListFormat format; format.setStyle(QTextListFormat::ListDecimal); ui->textEdit->textCursor().insertList(format); }
void MainWindow::insertImage() { QString filePath = QFileDialog::getOpenFileName(this,"选择图片",".","JPEG(*.jpg *.jpeg);;""GIF(*.gif);;""PNG(*.png)");
QUrl url(QString("file://%1").arg(filePath));
QImage image = QImageReader(filePath).read(); QTextDocument* document = ui->textEdit->document(); document->addResource(QTextDocument::ImageResource,url,QVariant(image)); QTextCursor cursor =ui->textEdit->textCursor(); QTextImageFormat imgFormat; imgFormat.setWidth(image.width()); imgFormat.setHeight(image.height()); imgFormat.setName(url.toString()); cursor.insertImage(imgFormat); }
|
语法高亮
![image-20220202233158144](https://gitee.com/Do2eM0N/blogimg/raw/master/202202022331193.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| void MySyntaxHighlighter::highlightBlock(const QString &text) { QTextCharFormat format; format.setFontWeight(QFont::Bold); format.setBackground(Qt::red); format.setForeground(Qt::green) ; QString pattern = "\\bgood\\b"; QRegExp expression(pattern); int index = text.indexOf(expression); while(index >= 0 ) { int length = expression.matchedLength(); setFormat(index,length,format); index = text.indexOf(expression,index + length); } }
|
字符查找
![image-20220203120216663](https://gitee.com/Do2eM0N/blogimg/raw/master/202202031227638.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| ``` QAction* action_textFind = new QAction("查找",this); connect(action_textFind,&QAction::triggered,this,&MainWindow::textFind); ui->mainToolBar->addAction(action_textFind); m_findDialg = new QDialog(this); m_lineEdit = new QLineEdit(m_findDialg); QPushButton* btn = new QPushButton(m_findDialg); btn->setText("查找下一个"); connect(btn,&QPushButton::clicked,this,&MainWindow::textNext); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(m_lineEdit); layout->addWidget(btn); m_findDialg->setLayout(layout); ``` void MainWindow::textFind() { m_findDialg->show(); } void MainWindow::textNext() { QString strFind = m_lineEdit->text(); bool isFind = ui->textEdit->find(strFind,QTextDocument::FindBackward); if(isFind) { qDebug()<<QString("行号:%1,列号:%2") .arg(ui->textEdit->textCursor().blockNumber()) .arg(ui->textEdit->textCursor().columnNumber()); } }
|