#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextFrame>
#include <QDebug>
#include <QLineEdit>
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTextDocument *document=ui->textEdit->document();
QTextFrame *rootFrame=document->rootFrame();
QTextFrameFormat format;
format.setBackground(Qt::blue);
format.setBorderBrush(Qt::red);
format.setBorder(3);
rootFrame->setFrameFormat(format);
QTextFrameFormat frameFormat;
frameFormat.setBackground(Qt::lightGray);
frameFormat.setMargin(20);
frameFormat.setPadding(50);
frameFormat.setBorder(6);
frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted);
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);
QAction *action_textFind=new QAction(tr("查找"),this);
connect(action_textFind,SIGNAL(triggered()),this,SLOT(textFind()));
ui->mainToolBar->addAction(action_textFind);
}
void MainWindow::showTextFrame()
{
QTextDocument *document=ui->textEdit->document();
QTextFrame *frame=document->rootFrame();
QTextFrame::iterator it;
for(it=frame->begin();it.atEnd()==0;++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();
qDebug()<<document->blockCount();
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)
{
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);
charFormat.setFont(QFont(tr("宋體"),12,QFont::Bold,true));
charFormat.setFontUnderline(true);
cursor.setCharFormat(charFormat);
cursor.insertText(tr("測試字體"));
}
}
void MainWindow::textFind()
{
QDialog *dlg=new QDialog(this);
lineEdit=new QLineEdit(dlg);
QPushButton *btn=new QPushButton(dlg);
btn->setText(tr("查找下一個"));
connect(btn,SIGNAL(clicked(bool)),this,SLOT(findNext()));
QVBoxLayout *layout=new QVBoxLayout;
layout->addWidget(lineEdit);
layout->addWidget(btn);
dlg->setLayout(layout);
dlg->show();
}
void MainWindow::findNext()
{
QString string=lineEdit->text();
bool isfind=ui->textEdit->find(string,QTextDocument::FindBackward);
if(isfind)
{
qDebug()<<tr("行號:%1列號:%2").arg(ui->textEdit->textCursor().blockNumber()).
arg(ui->textEdit->textCursor().columnNumber());
}
}
MainWindow::~MainWindow()
{
delete ui;
}
你不理解的地方,建議標志出來、並說明你的理解與問題。否則,別人給你全部注釋代碼,基本上是不可能的。