当前位置:网站首页>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 :

边栏推荐
- 2022r1 quick opening pressure vessel operation test questions and answers
- Jar package merging using Apache ant
- 移动安全工具-jarsigner
- Solution to network access packet loss of Tencent cloud international ECS
- Thingsboard - rest API obtains and refreshes tokens
- [redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem
- [redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly
- Garbage collection of C closure
- 傳輸層 以字節為單比特的滑動窗口技術
- Several ways for wechat applet to jump to the page are worth collecting
猜你喜欢

51单片机多机通信
Hyperledger Fabric 2. X dynamic update smart contract

2019 summary and 2020 outlook

VNC viewer remote connection raspberry pie without display

Sliding window technology based on byte in transport layer
最新QQ微信域名防红PHP程序源码+强制跳转打开

傳輸層 以字節為單比特的滑動窗口技術
![[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem](/img/01/5ec4e5dae1748dce3d5dee33a24b0b.png)
[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem

108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)

Thermodynamic diagram display correlation matrix
随机推荐
adb shell sendevent
Sanic服务启动失败
Basic summary of MySQL database knowledge
Can communication experiment between C and C
Leetcode 1248. Statistics of "graceful subarray" (harm, suddenly found that it can only enumerate violently)
Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model
adb shell sendevent
Usage of assert
What is test development? Can you find a job at this stage?
Apk decompiled method (not confused)
Helm chart warehouse operation
Text editor of QT project practice ---------- episode 8
December 6, 2019 what happens after the browser enters a URL
Decoupling pages and components using lifecycle
JSON file of China's provincial boundaries
Technologie des fenêtres coulissantes en octets dans la couche de transmission
Single blind box removal, social blind box and friend blind box program source code
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
ros(25):rqt_image_view报错Unable to load plugin for transport ‘compressed‘, error string
How to use promise Race() and promise any() ?