关键信息标红

效果:

        导入一个文本文件到textEdit中,对指定的key关键字标红处理或者对关键字所在的行进行整行标红处理

实现:       

#ifndef WIDGET_H
#define WIDGET_H

#include 

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


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文件,比较简单:



 Widget
 
  
   
    0
    0
    800
    600
   
  
  
   LogParseTool
  
  
   
    15
   
   
    
     
      6
     
     
      
       
        File:
       
      
     
     
      
     
     
      
       
        Select
       
      
     
     
      
       
        Qt::Horizontal
       
       
        QSizePolicy::Minimum
       
       
        
         20
         20
        
       
      
     
    
   
   
    
     
      Qt::NoContextMenu
     
     
      Qt::ScrollBarAlwaysOff
     
     
      Qt::ScrollBarAsNeeded
     
    
   
  
 
 
 

样式表,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;
}

你可能感兴趣的:(QtDemo,qt,文件处理,textedit,关键字查找,匹配)