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

 

原网站

版权声明
本文为[PureヾChan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210545036838.html