最近由于项目要求,需要显示软件操作日志且需要有搜索关键字功能。用到的控件是QPlainTextEdit,之所以不用QTextEdit显示文本,是因为当日志数据一多会造成程序卡顿(实测过,特别是用在嵌入式设备里面)。话不多说,直接上代码。
以下是关键代码:
.h文件
private:
QTextDocument *loginDoc;
int loginDocNum;
int loginCurrentDocNum;
.cpp文件
void searchText()
{
QString text = ui->lineEdit->text().trimmed();
if(nullptr == text)
{
myHelper::ShowMessageBoxError(QString("搜索内容不能为空!"));
return;
}
loginDoc = ui->txtMain->document();
loginDocNum = loginDoc->blockCount() ;
travPlainTextDoc(index,loginDocNum,loginDoc,text);
}
void travPlainTextDoc(int index,int docNum,QTextDocument *doc,QString text)
{
recoverExtraSelection(index);
for (int i = 0; i < docNum; i++)
{
QTextBlock textLine = doc->findBlockByNumber(i) ;
QString str = textLine.text();
if(str.contains(text))
{
QList<QTextEdit::ExtraSelection> extraSelections;
if(index == 0)
{
loginCurrentDocNum = i;
extraSelections = ui->txtMain->extraSelections();
ui->txtMain->setTextCursor(QTextCursor(textLine));
}
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor("#F79332");
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
if(index == 0)
{
selection.cursor = ui->txtMain->textCursor();
}
selection.cursor.clearSelection();
extraSelections.append(selection);
if(index == 0)
{
ui->txtMain->setExtraSelections(extraSelections);
}
return;
}
}
}
void recoverExtraSelection(int index)
{
QList<QTextEdit::ExtraSelection> selections;
if(index == 0)
{
selections = ui->txtMain->extraSelections();
}
for(int i = 0;i < selections.count();i++)
{
QColor lineColor = QColor("#646464");
selections.at(i).format.setBackground(lineColor);
selections.at(i).format.setProperty(QTextFormat::FullWidthSelection, true);
}
if(selections.count() > 0)
{
if(index == 0)
{
ui->txtMain->setExtraSelections(selections);
ui->txtMain->extraSelections().clear();
}
}
}
void on_btn_searchLast_clicked()
{
int index = ui->stackedWidget->currentIndex();
QString text = ui->lineEdit->text().trimmed();
if(nullptr == text)
{
myHelper::ShowMessageBoxError(QString("当前搜索内容为空!"));
return;
}
int currentDocNum = 0;
int docNum = 0;
QTextDocument *doc = nullptr;
if(index == 0)
{
currentDocNum = loginCurrentDocNum;
docNum = loginDocNum;
doc = loginDoc;
}
if(currentDocNum == 0)
{
return;
}
recoverExtraSelection(index);
for(int i = currentDocNum - 1;i < docNum;i--)
{
QTextBlock textLine = doc->findBlockByNumber(i) ;
QString str = textLine.text();
if(str.contains(text))
{
QList<QTextEdit::ExtraSelection> extraSelections;
if(index == 0)
{
loginCurrentDocNum = i;
extraSelections = ui->txtMain->extraSelections();
ui->txtMain->setTextCursor(QTextCursor(textLine));
}
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor("#F79332");
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
if(index == 0)
{
selection.cursor = ui->txtMain->textCursor();
}
selection.cursor.clearSelection();
extraSelections.append(selection);
if(index == 0)
{
ui->txtMain->setExtraSelections(extraSelections);
}
return;
}
}
}
void on_btn_searchNext_clicked()
{
int index = ui->stackedWidget->currentIndex();
QString text = ui->lineEdit->text().trimmed();
if(nullptr == text)
{
myHelper::ShowMessageBoxError(QString("当前搜索内容为空!"));
return;
}
int currentDocNum = 0;
int docNum = 0;
QTextDocument *doc = nullptr;
if(index == 0)
{
currentDocNum = loginCurrentDocNum;
docNum = loginDocNum;
doc = loginDoc;
}
if(currentDocNum >= docNum - 1)
{
myHelper::ShowMessageBoxError(QString(""));
return;
}
recoverExtraSelection(index);
for(int i = currentDocNum + 1;i < docNum;i++)
{
QTextBlock textLine = doc->findBlockByNumber(i) ;
QString str = textLine.text();
if(str.contains(text))
{
QList<QTextEdit::ExtraSelection> extraSelections;
if(index == 0)
{
loginCurrentDocNum = i;
extraSelections = ui->txtMain->extraSelections();
ui->txtMain->setTextCursor(QTextCursor(textLine));
}
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor("#F79332");
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
if(index == 0)
{
selection.cursor = ui->txtMain->textCursor();
}
selection.cursor.clearSelection();
extraSelections.append(selection);
if(index == 0)
{
ui->txtMain->setExtraSelections(extraSelections);
}
return;
}
}
}
上一个和下一个的代码未考虑到:当plainTextEdit新增数据后,上面的数据未更新的问题,自己改。
|