当前位置:网站首页>Text border format and text block of rich text
Text border format and text block of rich text
2022-06-25 01:02:00 【PureヾChan】
One 、 Border format
To create mainWIndows.h Add slot function :
private slots:
void showTextFrame();// Traverse text frame showTextFrame() Function implementation :
void MainWindow::showTextFrame()
{
QTextDocument *document=ui->textEdit->document();// Get document object
QTextFrame* frame=document->rootFrame();// Get the document frame
QTextFrame::iterator it;// Text frame iterator
for(it=frame->begin();it!=frame->end();it++){
// Get the current frame pointer
QTextFrame* childFrame=it.currentFrame();
// Get the current text block
QTextBlock childBlock=it.currentBlock();
if(childFrame){
qDebug()<<"frame:";
}else if(childBlock.isValid()){
qDebug()<<"block:"<<childBlock.text();// Print the text of the text block
}
}
}
The function of the slot function is to use document() Function to get a text object , Defining a QTextFrame Object to get the root frame of the text object , Then use iterator and root framework to traverse all frames and text blocks , Then print the contents of the text block , But we can't get specific information about each framework .
mainWIndow Constructor for :
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTextDocument* document=ui->textEdit->document();// Returns the text object of the document editor
// Get the root framework
QTextFrame* rootFrame=document->rootFrame();
// Document frame format
QTextFrameFormat format;
format.setBorderBrush(Qt::red);// Border color
format.setBorder(3);// Three pixels wide
// Document framework , Format
rootFrame->setFrameFormat(format);
// Set border style
QTextFrameFormat frameFormat;
frameFormat.setBackground(Qt::lightGray);// Set the background to bright gray
frameFormat.setMargin(10);// Set margins
frameFormat.setPadding(5);// Setting up the lining
frameFormat.setBorder(2);
frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_DotDash);// Set border style
QTextCursor cursor=ui->textEdit->textCursor();// Return the cursor to select the text information
cursor.insertFrame(frameFormat);
QAction* action_textFrame=new QAction(" frame ",this);
connect(action_textFrame,&QAction::triggered
,this,&MainWindow::showTextFrame);
action_textFrame->setCheckable(true);
ui->mainToolBar->addAction(action_textFrame);
}The constructor first defines a QTextDocument Object acquisition QTextEdit The text inside , In using the obtained text objects in obtaining their root framework , Then set the border color of the frame to red , And the width of the border is set to 3. Defining a subformat frameFormat Set the sub frame format background to bright gray , Set the distance from the root border to 10 Pixel , Set the inside margin ( The distance from the font to the inner border ) by 5 Pixel , Width is 2 The style is dotted line .
Define a QTextCursor Object to get cursor text information , Insert the previous sub format at the cursor . Let me define one more Action Add to the toolbar , And showTextFrame Function to correlate , Automatically call... After clicking .
Effect display :

EH , Some students may feel confused , Obviously I wrote 222222 Why is it not shown , Because it's time to traverse to the subframe , He can't read the text .
Two 、 Text block
To create mainWIndows.h Add slot function :
void showTextBlock();// Traversing text blocks
void setTextFont(bool checked);// Select to set the text font Function implementation :
void MainWindow::showTextBlock()
{
QTextDocument* document=ui->textEdit->document();
QTextBlock block=document->firstBlock();
for(int i=0;i<document->blockCount();i++){
//blockCount Get the number of text blocks
qDebug()<<QString(" Text block %1, The first line number of the text block is :%2, length :%3, Content :%4")
.arg(i)
.arg(block.firstLineNumber())
.arg(block.length())
.arg(block.text());
}
}
void MainWindow::setTextFont(bool checked)
{
if(checked){
// If you choose
QTextCursor cursor=ui->textEdit->textCursor();// Get cursor selection information
QTextBlockFormat blockFormat;
// Align center
blockFormat.setAlignment(Qt::AlignCenter);
cursor.insertBlock(blockFormat);// Insert text block format
// Character format
QTextCharFormat charFormat;
charFormat.setBackground(Qt::lightGray);
charFormat.setForeground(Qt::blue);// Set the foreground color to blue
charFormat.setFont(QFont(" Song style ",12,QFont::Bold,true));
charFormat.setFontUnderline(true);// Underline
cursor.setCharFormat(charFormat);
cursor.insertText(" Cheerleading ");
}
}
showTextBlock Function takes advantage of the obtained text object document call blcokCount Get the number of text blocks , recycling for Loop implementation traversal reaches , Output the specific information of each text block .
blocktext() Is to get the information of the text block .
setTextFont Is a function to format text fonts , First, judge whether the button is selected , Define a mouse object to get the selected text . Then define a format object and set the alignment method to center ,insertBlock You can insert the specified format . Define a font format object , Set up a series of font formats to use setCharFormat The text format is set just under the cursor .
insertText The function inserts text under the cursor .
mainwindows Constructor adds code :
// Add buttons to the toolbar
QAction* action_textBlcok=new QAction(" Text block ",this);
connect(action_textBlcok,&QAction::triggered,
this,&MainWindow::showTextBlock);
ui->mainToolBar->addAction(action_textBlcok);
QAction* action_textFont=new QAction(" typeface ",this);
action_textFont->setCheckable(true);
connect(action_textFont,&QAction::triggered
,this,&MainWindow::setTextFont);
ui->mainToolBar->addAction(action_textFont);
Here are two definitions Action Buttons are added to the main toolbar , And establish a relationship with the slot function of the response . But the font button here should be set whether to check , Because your slot function has a parameter Checked, Therefore, you can check if you want to set .
Effect display :

边栏推荐
- Usage of assert
- Custom control - round dot progress bar (imitating one key acceleration in security guard)
- 【微服务|Sentinel】簇点链路|微服务集群环境搭建
- Thermodynamic diagram display correlation matrix
- [Solved] Public key for mysql-community-xxx. rpm is not installed
- Applet opening traffic master
- I 刷题 I — 复制带随机指针的链表
- Mobile security tool jarsigner
- 51单片机多机通信
- Single blind box removal, social blind box and friend blind box program source code
猜你喜欢

不重要的token可以提前停止计算!英伟达提出自适应token的高效视觉Transformer网络A-ViT,提高模型的吞吐量!...

Tiktok wallpaper applet source code
![[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS](/img/fc/6b36759570b9b1007d2640009576d9.png)
[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS

Go crawler framework -colly actual combat (II) -- Douban top250 crawling

2022r1 quick opening pressure vessel operation test questions and answers

Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar

Use of file class filenamefilter & filefilter in io

2021-11-07

百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑

Custom animation (simulated win10 loading animation)
随机推荐
In the process of enterprise development, I found that a colleague used the select * from where condition for update
Xcode预览(Preview)显示List视图内容的一个Bug及解决
A plug-in framework for implementing registration free and login verification with hook technology
Thingsboard - rest API obtains and refreshes tokens
百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑
December 6, 2019 what happens after the browser enters a URL
Tiktok wallpaper applet v1.0.2 function, new arrival function
实现mnist手写数字识别
Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
移动安全工具-dex2jar
If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?
Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
JSON file of China's provincial boundaries
Punch smart spirit 1. The brand is attractive. What is the strength of the product?
2022 melting welding and thermal cutting recurrent training question bank simulated examination platform operation
Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
JS dynamically generates variable names and assigns values
Registration method of native method in JNI
断言(assert)的用法
Tiktok wallpaper applet, starlight wallpaper applet version 2.0, upgraded version