QT study notes (2)

QT Chapter 5-5.2 Text Block
Complete study CODE:
1).h file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
   
public:
    explicit MainWindow(QWidget * parent = 0);
    ~MainWindow();
   
private:
    Ui::MainWindow *ui;
private slots:
    void showTextFrame();
    void showTextBlock();
    void setTextFont(bool checked);

};

#endif // MAINWINDOW_H

2).CPP File:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec> #include
<QTextFrame>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this); QTextCodec
   :: setCodecForTr(QTextCodec::codecForName("UTF-8"));
    // Practical application of QT framework
    QTextDocument *document = ui->textEdit->document();//Get document object
    QTextFrame *rootFrame = document->rootFrame( );//Get the root frame
    QTextFrameFormat format;//Create a frame format
    format.setBorderBrush(Qt::red);//Border color
    format.setBorder(3);//Border width
    rootFrame->setFrameFormat(format); //The frame uses the format

    //Using the cursor class object, add a subframe
    QTextFrameFormat frameFormat in the root frame;
    frameFormat.setBackground(Qt::lightGray);//Set the background color
    frameFormat.setMargin(10);//Set the margin
    frameFormat.setPadding(5);//Set the padding
    frameFormat.setBorder(2);
    frameFormat .setBorderStyle(QTextFrameFormat::BorderStyle_Groove);
    QTextCursor cursor = ui->textEdit->textCursor();
    cursor.insertFrame(frameFormat);

    QAction *action_textFrame = new QAction(tr("frame"), this);
    connect(action_textFrame, SIGNAL(triggered()), this, SLOT(showTextFrame()));
    ui->mainToolBar->addAction(action_textFrame);//Add an action QAction to the toolbar

    *action_textBlock = new QAction(tr("text block") , this);
    connect(action_textBlock, SIGNAL(triggered()), this, SLOT(showTextBlock()));
    ui->mainToolBar->addAction(action_textBlock);

    QAction *action_font = new QAction(tr("字体"), this);
    action_font -> setCheckable(true);
    connect(action_font, SIGNAL(toggled(bool)), this, SLOT(setTextFont(bool)));
    ui->mainToolBar->addAction(action_font);


}


MainWindow::~MainWindow()
{
    delete ui;
}

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();//Get the pointer of the current frame
        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()<<tr("文本块%1,文本块首行行号为:%2, 长度为:%3, 内容为:").arg(i).arg(block.firstLineNumber()).arg(block.length())<<block.text();
       block = block.next();
   }
}

void MainWindow::setTextFont(bool checked)
{
    QTextCursor cursor;
    QTextCharFormat charFormat;

    if(checked)
    {
        cursor = ui->textEdit->textCursor();
        QTextBlockFormat blockFormat;
        blockFormat.setAlignment(Qt::AlignCenter);
        cursor.insertBlock(blockFormat);
        charFormat.setBackground(Qt::lightGray);
        charFormat.setForeground(Qt::blue);
        //使用宋体,12号,加粗,倾斜
        charFormat.setFont(QFont(tr("宋体"), 12, QFont::Bold, true));
        charFormat.setFontUnderline(true);
        cursor.setCharFormat(charFormat);
        cursor.insertText(tr("Test Font"));
    }
    else
    {
        charFormat.setFont(QFont(tr("宋体"), 10, QFont::Black, true));
        charFormat.setFontUnderline (false);
        cursor.setCharFormat(charFormat);
        cursor.insertText(tr("Test font"));

    }

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326377414&siteId=291194637