1. Qt對富文本的處理
1.1 操作方式
編輯操作:使用基於光標的接口函數,模擬用戶的編輯操作,且不丟失底層文檔框架;文檔的光標基於QTextCursor類
只讀操作:使用了只讀的分層次接口函數,有利於文檔的檢索和輸出;文檔的框架基於QTextDocument
1.2 富文本文檔的結構
結構元素:框架(QTextFrame),文本塊(QTextBlock),表格(QTextTable),列表(QTextList)
對應格式:框架格式(QTextFrameFormat),文本塊格式(QTextBlockFormat),表格格式(QTextTableFormat),列表格式(QTextListFrame)
格式是在編輯文檔時與QTextCursor配合使用
1.3QTextEdit是一個富文本編輯器,在構建QTextEdit對象時就已經構建了一個QTextDocument對象和一個QTextCursor對象
空文檔:包含根框架(rootFrame),文本塊(Block)
QTextDocument *doc = ui->textEdit->document(); //獲取文檔對象
QTextFrame *rootFrame = doc->rootFrame(); //獲取根框架
QTextFrameFormat frameFormat; //創建框架格式
frameFormat.setBorderBrush(Qt::red); //設置邊界顏色
frameFormat.setBorder(5); //設置邊界寬度
rootFrame->setFrameFormat(frameFormat); //給框架使用格式
QTextFrameFormat frameFormat2;
frameFormat2.setBackground(Qt::lightGray); //設置背景色
frameFormat2.setMargin(10); //設置邊距
frameFormat2.setPadding(5); //設置填充
frameFormat2.setBorder(2);
frameFormat2.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted); //設置邊框樣式
QTextCursor cursor = ui->textEdit->textCursor(); //獲取光標
cursor.insertFrame(frameFormat2); //在光標處插入框架1.4 文本塊(QTextBlock)
1.4.1遍歷文檔框架
1.4.1.1 添加槽函數(在主界面頭文件中添加)
private slots:
void showTextFrame(); //遍歷文檔框架1.4.1.2 在工具欄上添加Action
QAction * act = new QAction(QString("框架"),this);
connect(act,SIGNAL(triggered()),this,SLOT(showTextFrame()));
ui->mainToolBar->addAction(act);1.4.1.3 添加槽函數的實現代碼
void MainWindow::showTextFrame()
{
QTextDocument * doc = ui->textEdit->document();
QTextFrame *frame = doc->rootFrame();
QTextFrame::iterator it; //建立QTextFrame類的迭代器
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();
}
}
1.4.2 遍歷文本塊
1.4.2.1 添加槽函數(在主界面頭文件中添加)
void showTextBlock(); //遍歷所有文本塊
1.4.2.2 在工具欄上添加Action
QAction * act_textBlock = new QAction(QString("文本塊"),this);
connect(act_textBlock,SIGNAL(triggered()),this,SLOT(showTextBlock()));
ui->mainToolBar->addAction(act_textBlock);
1.4.2.3 添加槽函數的實現代碼
void MainWindow::showTextBlock()
{
QTextDocument* doc = ui->textEdit->document();
QTextBlock block = doc->firstBlock(); //獲取文檔的第一個文本塊
for(int i = 0; i < doc->blockCount();i++) {
qDebug() << QString("文本塊%1,文本塊首行行號爲:%2,長度爲:%3,內容爲:")
.arg(i).arg(block.firstLineNumber()).arg(block.length())
<< block.text();
block = block.next();
}
}1.4.3 設置字體格式
1.4.3.1 添加槽函數(在主界面頭文件中添加)
void setTextFont(bool checked); //設置字體格式
1.4.3.2 在工具欄上添加Action
//設置字體
QAction * act_font = new QAction(QString("字體"),this);
act_font->setCheckable(true);
connect(act_font,SIGNAL(toggled(bool)),this,SLOT(setTextFont(bool)));
ui->mainToolBar->addAction(act_font);
1.4.3.3 添加槽函數的實現代碼
void MainWindow::setTextFont(bool checked)
{
if (checked) {
QTextCursor cursor = ui->textEdit->textCursor();
//插入文本塊
QTextBlockFormat blockFormat;
blockFormat.setAlignment(Qt::AlignCenter);
cursor.insertBlock(blockFormat);
//字符格式,字符顏色
QTextCharFormat charFormat;
charFormat.setBackground(Qt::lightGray);
charFormat.setForeground(Qt::blue);
//宋體,12號,加粗,傾斜
charFormat.setFont(QFont(QString("宋體"),12,QFont::Bold,true));
charFormat.setFontUnderline(true);
cursor.setCharFormat(charFormat);
cursor.insertText(QString("測試字體"));
}
}
1.4.4 插入表格,列表,圖片
1.4.4.1 添加槽函數(在主界面頭文件中添加)
void insertTable(); //插入表格
void insertList(); //插入列表
void insertImage(); //插入圖片
1.4.4.2 在工具欄上添加Action
//插入表格
QAction * act_insertTable = new QAction(QString("插入表格"),this);
connect(act_insertTable,SIGNAL(triggered()),this,SLOT(insertTable()));
ui->mainToolBar->addAction(act_insertTable);
//插入列表
QAction * act_insertList = new QAction(QString("插入列表"),this);
connect(act_insertList,SIGNAL(triggered()),this,SLOT(insertList()));
ui->mainToolBar->addAction(act_insertList);
//插入圖片
QAction * act_insertImage = new QAction(QString("插入圖片"),this);
connect(act_insertImage,SIGNAL(triggered()),this,SLOT(insertImage()));
ui->mainToolBar->addAction(act_insertImage);
1.4.4.3 添加槽函數的實現代碼
void MainWindow::insertTable()
{
QTextCursor cursor = ui->textEdit->textCursor();
QTextTableFormat tableFormat;
tableFormat.setCellPadding(10);
tableFormat.setCellSpacing(2);
cursor.insertTable(2,2,tableFormat);
}
void MainWindow::insertList()
{
QTextCursor cursor = ui->textEdit->textCursor();
QTextListFormat listFormat;
listFormat.setStyle(QTextListFormat::ListDecimal);
cursor.insertList(listFormat);
}
void MainWindow::insertImage()
{
QTextImageFormat imageFormat;
imageFormat.setName(":/image/2.jpg");
ui->textEdit->textCursor().insertImage(imageFormat);
}
1.4.5 查找
1.4.5.1 前置聲明QLineEdit,在界面頭文件中前置聲明
class QLineEdit;1.4.5.2 添加私有對象,前置聲明就是爲了這裏使用的
QLineEdit *lineEdit;
1.4.5.3 添加私有槽函數
void textFind(); //文本查找
void findNext(); //查找下一個1.4.5.4 添加action
//查找
QAction *act_find = new QAction(QString("查找"),this);
connect(act_find,SIGNAL(triggered()),this,SLOT(textFind()));
ui->mainToolBar->addAction(act_find);
1.4.5.5 實現槽函數
void MainWindow::textFind() //查找文本
{
QDialog *dlg = new QDialog(this); //創建查找對話框
lineEdit = new QLineEdit(dlg); //創建字符串輸入框
QPushButton* btFindNext = new QPushButton(this); //查找按鈕
btFindNext->setText(QString("查找下一個"));
connect(btFindNext,SIGNAL(clicked()),this,SLOT(findNext())); //關聯查找信號和槽
QVBoxLayout* layout = new QVBoxLayout; //垂直佈局
layout->addWidget(lineEdit); //將控件添加到主界面
layout->addWidget(btFindNext);
dlg->setLayout(layout);
dlg->show();
}
void MainWindow::findNext()
{
QString string = lineEdit->text();
//查找字符串,查找標誌:QTextDocument::FindBackward:向前查找,
//FindWholeWords:整個文檔查找,FindCaseSensitively:忽略大小寫查找
bool isFind = ui->textEdit->find(string,QTextDocument::FindWholeWords);
if (isFind) {
qDebug() << QString("行號:%1,列號:%2").arg(ui->textEdit->textCursor().blockNumber())
.arg(ui->textEdit->textCursor().columnNumber());
}
}1.4.6語法高亮與html
1.4.6.1添加新的文件,選擇C++ 類,類名:mySyntaxHighlighter,基類:QSyntaxHighlighter,繼承自:QObject
1.4.6.2 修改mysyntaxhighlighter.h頭文件
將QTextDocument類對象指針作爲其父部件指針,這樣可以自動調用highlightBlock()函數,可以時的檢測文本
//將QTextDocument作爲父窗口這樣就可以自動調用highlightBlock()函數
explicit mySyntaxHighlighter(QTextDocument *parent = 0);重新實現highlightBlock()函數以便將,字符串的格式應用到特定的字符串上面
protected :
void highlightBlock(const QString &text); //必須重新實現該函數1.4.6.3 修改mysyntaxhighlighter.cpp文件
mySyntaxHighlighter::mySyntaxHighlighter(QTextDocument *parent) :
QSyntaxHighlighter(parent)
{
}
void mySyntaxHighlighter::highlightBlock(const QString &text)
{
QTextCharFormat charFormat; //設置匹配的字符格式
charFormat.setFontWeight(QFont::Bold);
charFormat.setForeground(Qt::green);
QString pattern = "\\bchar\\b"; //要匹配的字符,這裏是char單詞
QRegExp expression(pattern); //創建正則表達式
int index = text.indexOf(expression); //默認從0開始匹配字符串
//如果匹配成功,返回值爲字符串的起始位置,它大於或等於0
while(index >= 0 ) {
int length = expression.matchedLength(); //要匹配字符串的長度
setFormat(index,length,charFormat); //對要匹配的字符串設置格式
index = text.indexOf(expression,index + length); //繼續匹配
}
}
1.4.6.4 修該主界面頭文件
class mySyntaxHighlighter;
添加私有變量
mySyntaxHighlighter *highlighter;1.4.6.5 修該主界面的cpp文件
在構造函數中添加
highlighter = new mySyntaxHighlighter(ui->textEdit->document());
1.4.7 QTextEdit的使用
1.4.7.1 建立一個QTextEdit
QTextDocument *m_doc = new QTextDocument("文本框中的文本");//創建一個裝文本的容器
QTextEdit te;//定義文本框
te.setDocument(m_doc);//爲文本框綁定內容
1.4.7.2 向QTextEdit中寫入數據
QString gyyq = rec.value("gy").toString();
te.document()->setPlainText(gyyq);
QString gyyq = te.document()->toPlainText();