• 关键信息标红


    效果:

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

    实现:       

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include <QWidget>
    4. QT_BEGIN_NAMESPACE
    5. namespace Ui { class Widget; }
    6. QT_END_NAMESPACE
    7. class Widget : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. Widget(QWidget *parent = nullptr);
    12. ~Widget();
    13. private slots:
    14. void on_selectBtn_clicked();
    15. //void on_setBtn_clicked();
    16. private:
    17. void defaultConfig();
    18. void loadStyleSheet(const QString &style);
    19. void filterKeyWord(const QString& key, const QString& color);
    20. private:
    21. Ui::Widget *ui;
    22. QString mAppPath;
    23. };
    24. #endif // WIDGET_H
    25. cpp //
    26. #include "widget.h"
    27. #include "ui_widget.h"
    28. #include <QDebug>
    29. #include <QDir>
    30. #include <QFile>
    31. #include <QStyle>
    32. #include <QSettings>
    33. #include <QTextStream>
    34. #include <QFileDialog>
    35. #include <QMessageBox>
    36. #include <QTextDocument>
    37. Widget::Widget(QWidget *parent)
    38. : QWidget(parent)
    39. , ui(new Ui::Widget)
    40. {
    41. ui->setupUi(this);
    42. defaultConfig();
    43. loadStyleSheet(":/home.qss");
    44. }
    45. Widget::~Widget()
    46. {
    47. delete ui;
    48. }
    49. void Widget::loadStyleSheet(const QString &style)
    50. {
    51. if(style.isEmpty())
    52. return;
    53. QFile file(style);
    54. if(!file.open(QFile::ReadOnly))
    55. return;
    56. this->style()->unpolish(this);
    57. setStyleSheet(file.readAll());
    58. this->style()->polish(this);
    59. }
    60. void Widget::on_selectBtn_clicked()
    61. {
    62. QSettings config(mAppPath+"/setting.ini", QSettings::NativeFormat);
    63. auto lastPath = config.value("last_path", QDir::homePath()).toString();
    64. auto file = QFileDialog::getOpenFileName(this, tr("OpenFile"), lastPath,
    65. tr("Text(*.txt);; All file(*.*)"));
    66. if(file.isEmpty())
    67. return;
    68. QFile inputFile(file);
    69. inputFile.open(QIODevice::ReadOnly);
    70. QTextStream in(&inputFile);
    71. in.setCodec("UTF-8");
    72. ui->textEdit->setText(in.readAll());
    73. inputFile.close();
    74. config.setValue("last_path", file);
    75. ui->path->setText(file);
    76. //ui->textEdit->document()->undo();
    77. config.beginGroup("keys");
    78. auto kk = config.childKeys();
    79. foreach (auto key, config.childKeys())
    80. filterKeyWord(key, config.value(key, "#000").toString());
    81. config.endGroup();
    82. }
    83. void Widget::defaultConfig()
    84. {
    85. mAppPath = QApplication::applicationDirPath();
    86. QSettings config(mAppPath+"/setting.ini", QSettings::NativeFormat);
    87. config.beginGroup("keys");
    88. if(config.childKeys().isEmpty())
    89. {
    90. config.setValue("ERROR", "#F00");
    91. config.setValue("FATAL", "#FF0");
    92. }
    93. config.endGroup();
    94. }
    95. void Widget::filterKeyWord(const QString &key, const QString &color)
    96. {
    97. QTextDocument *document = ui->textEdit->document();
    98. bool found = false;
    99. // undo previous change (if any)
    100. //document->undo();
    101. if (key.isEmpty()) {
    102. QMessageBox::information(this, tr("Empty Search Field"),
    103. tr("The search field is empty. "
    104. "Please enter a word and click Find."));
    105. } else {
    106. QTextCursor highlightCursor(document);
    107. QTextCursor cursor(document);
    108. cursor.beginEditBlock();
    109. //! [6]
    110. QTextCharFormat plainFormat(highlightCursor.charFormat());
    111. QTextCharFormat colorFormat = plainFormat;
    112. colorFormat.setForeground(QColor(color));
    113. while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
    114. highlightCursor = document->find(key, highlightCursor,
    115. QTextDocument::FindWholeWords);
    116. if (!highlightCursor.isNull()) {
    117. found = true;
    118. //如果只是对关键字选中,则放开下面注释,并将下面select函数调用行注释掉
    119. // highlightCursor.movePosition(QTextCursor::WordRight,
    120. // QTextCursor::KeepAnchor);
    121. highlightCursor.select(QTextCursor::LineUnderCursor);
    122. highlightCursor.mergeCharFormat(colorFormat);
    123. }
    124. }
    125. //! [8]
    126. cursor.endEditBlock();
    127. //! [7] //! [9]
    128. if (found == false) {
    129. QMessageBox::information(this, tr("Word Not Found"),
    130. tr("Sorry, the word cannot be found."));
    131. }
    132. }
    133. }

    ui文件,比较简单:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>Widget</class>
    4. <widget class="QWidget" name="Widget">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>800</width>
    10. <height>600</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>LogParseTool</string>
    15. </property>
    16. <layout class="QVBoxLayout" name="verticalLayout" stretch="0,1">
    17. <property name="spacing">
    18. <number>15</number>
    19. </property>
    20. <item>
    21. <layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1,0,0">
    22. <property name="spacing">
    23. <number>6</number>
    24. </property>
    25. <item>
    26. <widget class="QLabel" name="label">
    27. <property name="text">
    28. <string>File:</string>
    29. </property>
    30. </widget>
    31. </item>
    32. <item>
    33. <widget class="QLineEdit" name="path"/>
    34. </item>
    35. <item>
    36. <widget class="QPushButton" name="selectBtn">
    37. <property name="text">
    38. <string>Select</string>
    39. </property>
    40. </widget>
    41. </item>
    42. <item>
    43. <spacer name="horizontalSpacer">
    44. <property name="orientation">
    45. <enum>Qt::Horizontal</enum>
    46. </property>
    47. <property name="sizeType">
    48. <enum>QSizePolicy::Minimum</enum>
    49. </property>
    50. <property name="sizeHint" stdset="0">
    51. <size>
    52. <width>20</width>
    53. <height>20</height>
    54. </size>
    55. </property>
    56. </spacer>
    57. </item>
    58. </layout>
    59. </item>
    60. <item>
    61. <widget class="QTextEdit" name="textEdit">
    62. <property name="contextMenuPolicy">
    63. <enum>Qt::NoContextMenu</enum>
    64. </property>
    65. <property name="verticalScrollBarPolicy">
    66. <enum>Qt::ScrollBarAlwaysOff</enum>
    67. </property>
    68. <property name="horizontalScrollBarPolicy">
    69. <enum>Qt::ScrollBarAsNeeded</enum>
    70. </property>
    71. </widget>
    72. </item>
    73. </layout>
    74. </widget>
    75. <resources/>
    76. <connections/>
    77. </ui>

    样式表,qss文件:

    1. #Widget
    2. {
    3. background-color: #4A4A4A;
    4. }
    5. #Widget #label
    6. {
    7. color: #fff;
    8. font-weight: bold;
    9. }
    10. #path
    11. {
    12. color: #fff;
    13. border-top: 0px solid red;
    14. border-bottom: 1px solid #cdcdcd;
    15. border-left: 0px solid red;
    16. border-right: 0px solid red;
    17. background: transparent;
    18. }
    19. #textEdit
    20. {
    21. border-top: 0px solid red;
    22. border-bottom: 1px solid #cdcdcd;
    23. border-left: 1px solid #cdcdcd;
    24. border-right: 1px solid #cdcdcd;
    25. border: none;
    26. background-color: #4A4A4A;
    27. }
    28. #selectBtn, #setBtn
    29. {
    30. color: black;
    31. background: #2d6699;
    32. }

  • 相关阅读:
    java中有哪些并发的List?只知道一种的就太逊了
    代码随想录算法训练营第五十天|股票问题专题(2)
    2月21日,每日信息差
    生成对抗网络入门案例
    带研发团队后的日常思考1 初级管理者的困惑
    文件包含漏洞及漏洞复现
    Web 基于ECharts数据可视化
    Java面试题-Redis-第一天(Redis简单介绍)
    vue2 项目中引入iconfont
    【Mybatis】浅谈延迟加载
  • 原文地址:https://blog.csdn.net/weixin_43935710/article/details/136511284