效果:
导入一个文本文件到textEdit中,对指定的key关键字标红处理或者对关键字所在的行进行整行标红处理
实现:
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
-
- private slots:
- void on_selectBtn_clicked();
- //void on_setBtn_clicked();
-
- private:
- void defaultConfig();
- void loadStyleSheet(const QString &style);
- void filterKeyWord(const QString& key, const QString& color);
-
- private:
- Ui::Widget *ui;
- QString mAppPath;
- };
- #endif // WIDGET_H
-
-
- cpp //
-
- #include "widget.h"
- #include "ui_widget.h"
-
- #include <QDebug>
- #include <QDir>
- #include <QFile>
- #include <QStyle>
- #include <QSettings>
- #include <QTextStream>
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QTextDocument>
-
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- defaultConfig();
- loadStyleSheet(":/home.qss");
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::loadStyleSheet(const QString &style)
- {
- if(style.isEmpty())
- return;
-
- QFile file(style);
- if(!file.open(QFile::ReadOnly))
- return;
-
- this->style()->unpolish(this);
- setStyleSheet(file.readAll());
- this->style()->polish(this);
- }
-
- void Widget::on_selectBtn_clicked()
- {
- QSettings config(mAppPath+"/setting.ini", QSettings::NativeFormat);
- auto lastPath = config.value("last_path", QDir::homePath()).toString();
- auto file = QFileDialog::getOpenFileName(this, tr("OpenFile"), lastPath,
- tr("Text(*.txt);; All file(*.*)"));
- if(file.isEmpty())
- return;
-
- QFile inputFile(file);
- inputFile.open(QIODevice::ReadOnly);
- QTextStream in(&inputFile);
- in.setCodec("UTF-8");
- ui->textEdit->setText(in.readAll());
- inputFile.close();
-
- config.setValue("last_path", file);
- ui->path->setText(file);
- //ui->textEdit->document()->undo();
-
- config.beginGroup("keys");
- auto kk = config.childKeys();
- foreach (auto key, config.childKeys())
- filterKeyWord(key, config.value(key, "#000").toString());
- config.endGroup();
- }
-
- void Widget::defaultConfig()
- {
- mAppPath = QApplication::applicationDirPath();
- QSettings config(mAppPath+"/setting.ini", QSettings::NativeFormat);
- config.beginGroup("keys");
- if(config.childKeys().isEmpty())
- {
- config.setValue("ERROR", "#F00");
- config.setValue("FATAL", "#FF0");
- }
- config.endGroup();
- }
-
- void Widget::filterKeyWord(const QString &key, const QString &color)
- {
- QTextDocument *document = ui->textEdit->document();
-
- bool found = false;
-
- // undo previous change (if any)
- //document->undo();
-
- if (key.isEmpty()) {
- QMessageBox::information(this, tr("Empty Search Field"),
- tr("The search field is empty. "
- "Please enter a word and click Find."));
- } else {
- QTextCursor highlightCursor(document);
- QTextCursor cursor(document);
-
- cursor.beginEditBlock();
- //! [6]
-
- QTextCharFormat plainFormat(highlightCursor.charFormat());
- QTextCharFormat colorFormat = plainFormat;
- colorFormat.setForeground(QColor(color));
-
- while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
- highlightCursor = document->find(key, highlightCursor,
- QTextDocument::FindWholeWords);
-
- if (!highlightCursor.isNull()) {
- found = true;
- //如果只是对关键字选中,则放开下面注释,并将下面select函数调用行注释掉
- // highlightCursor.movePosition(QTextCursor::WordRight,
- // QTextCursor::KeepAnchor);
- highlightCursor.select(QTextCursor::LineUnderCursor);
- highlightCursor.mergeCharFormat(colorFormat);
- }
- }
-
- //! [8]
- cursor.endEditBlock();
- //! [7] //! [9]
-
- if (found == false) {
- QMessageBox::information(this, tr("Word Not Found"),
- tr("Sorry, the word cannot be found."));
- }
- }
- }
ui文件,比较简单:
- <?xml version="1.0" encoding="UTF-8"?>
- <ui version="4.0">
- <class>Widget</class>
- <widget class="QWidget" name="Widget">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>800</width>
- <height>600</height>
- </rect>
- </property>
- <property name="windowTitle">
- <string>LogParseTool</string>
- </property>
- <layout class="QVBoxLayout" name="verticalLayout" stretch="0,1">
- <property name="spacing">
- <number>15</number>
- </property>
- <item>
- <layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1,0,0">
- <property name="spacing">
- <number>6</number>
- </property>
- <item>
- <widget class="QLabel" name="label">
- <property name="text">
- <string>File:</string>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QLineEdit" name="path"/>
- </item>
- <item>
- <widget class="QPushButton" name="selectBtn">
- <property name="text">
- <string>Select</string>
- </property>
- </widget>
- </item>
- <item>
- <spacer name="horizontalSpacer">
- <property name="orientation">
- <enum>Qt::Horizontal</enum>
- </property>
- <property name="sizeType">
- <enum>QSizePolicy::Minimum</enum>
- </property>
- <property name="sizeHint" stdset="0">
- <size>
- <width>20</width>
- <height>20</height>
- </size>
- </property>
- </spacer>
- </item>
- </layout>
- </item>
- <item>
- <widget class="QTextEdit" name="textEdit">
- <property name="contextMenuPolicy">
- <enum>Qt::NoContextMenu</enum>
- </property>
- <property name="verticalScrollBarPolicy">
- <enum>Qt::ScrollBarAlwaysOff</enum>
- </property>
- <property name="horizontalScrollBarPolicy">
- <enum>Qt::ScrollBarAsNeeded</enum>
- </property>
- </widget>
- </item>
- </layout>
- </widget>
- <resources/>
- <connections/>
- </ui>
样式表,qss文件:
- #Widget
- {
- background-color: #4A4A4A;
- }
-
- #Widget #label
- {
- color: #fff;
- font-weight: bold;
- }
-
- #path
- {
- color: #fff;
- border-top: 0px solid red;
- border-bottom: 1px solid #cdcdcd;
- border-left: 0px solid red;
- border-right: 0px solid red;
- background: transparent;
- }
-
- #textEdit
- {
- border-top: 0px solid red;
- border-bottom: 1px solid #cdcdcd;
- border-left: 1px solid #cdcdcd;
- border-right: 1px solid #cdcdcd;
- border: none;
- background-color: #4A4A4A;
- }
-
- #selectBtn, #setBtn
- {
- color: black;
- background: #2d6699;
- }