QT学习笔记(二)
程序员文章站
2022-06-01 20:15:00
...
QT 第五章-5.2文本块
完整学习CODE:
1).h文件:
#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文件:
#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"));
// QT 框架的实际应用
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_Groove);
QTextCursor cursor = ui->textEdit->textCursor();
cursor.insertFrame(frameFormat);
QAction *action_textFrame = new QAction(tr("框架"), this);
connect(action_textFrame, SIGNAL(triggered()), this, SLOT(showTextFrame()));
ui->mainToolBar->addAction(action_textFrame);//在工具栏新增动作
QAction *action_textBlock = new QAction(tr("文本块"), 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();//获取当前框架的指针
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("测试字体"));
}
else
{
charFormat.setFont(QFont(tr("宋体"), 10, QFont::Black, true));
charFormat.setFontUnderline(false);
cursor.setCharFormat(charFormat);
cursor.insertText(tr("测试字体"));
}
}
完整学习CODE:
1).h文件:
#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文件:
#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"));
// QT 框架的实际应用
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_Groove);
QTextCursor cursor = ui->textEdit->textCursor();
cursor.insertFrame(frameFormat);
QAction *action_textFrame = new QAction(tr("框架"), this);
connect(action_textFrame, SIGNAL(triggered()), this, SLOT(showTextFrame()));
ui->mainToolBar->addAction(action_textFrame);//在工具栏新增动作
QAction *action_textBlock = new QAction(tr("文本块"), 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();//获取当前框架的指针
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("测试字体"));
}
else
{
charFormat.setFont(QFont(tr("宋体"), 10, QFont::Black, true));
charFormat.setFontUnderline(false);
cursor.setCharFormat(charFormat);
cursor.insertText(tr("测试字体"));
}
}