1.1 封裝高亮顯示檢索關鍵字控件
需要對搜索的關鍵字進行高亮顯示,而QLabel只能通過setstytlesheet函數去設置一種字體顏色,無法實現一行文字多種顏色,像這種名字中把搜索關鍵字高亮顯示的需求,無法用簡單的QLabel實現,可以通過QTextDocument加載html腳本,來實現檢索關鍵詞高亮顯示,有多個關鍵詞時,都會高亮顯示;封裝成一個類,只需要設置進去顯示文字和高亮文字就可以了。實現方法如下所示:
(1)頭文件
#ifndef COLLORLABEL_H #define COLLORLABEL_H #include <QObject> #include <QLabel> #include <QTextDocument> class CollorLabel : public QLabel { Q_OBJECT public: CollorLabel(); ~CollorLabel(); void paintEvent(QPaintEvent *event); void SetName(QString strName) { m_strName = strName; }; void SetfilterHover(QString filterHover) { m_filterHover = filterHover; }; private: QString m_strName=""; QString m_filterHover=""; QTextDocument* m_LabelName; }; #endif // COLLORLABEL_H
(2)源文件
#include "CollorLabel.h" #include <QPainter> #include <QColor> #include <QBrush> #include <QRect> #include <QPoint> CollorLabel::CollorLabel() { m_LabelName=new QTextDocument(this); m_LabelName->setUndoRedoEnabled(false); m_LabelName->setDocumentMargin(0); //背景色設置爲透明,這樣鼠標放入就有hover的顏色 setStyleSheet(QLatin1String("QLabel\n" "{\n" " font-family: Microsoft YaHei UI;\n" " font-size: 16px;\n" " color: rgba(255,255,255,0.70);\n" " letter-spacing: 0;\n" " line-height: 24px;\n" " background: rgba(255,255,255,0);\n" " background:transparent;\n" "}\n" "QToolTip\n" "{\n" " font-size:14px;\n" " width:1024;\n" " height:40;\n" " font-family: Microsoft YaHei UI;\n" "}")); setAttribute(Qt::WA_TranslucentBackground); } CollorLabel::~CollorLabel() { } void CollorLabel::paintEvent(QPaintEvent *event) { QPainter ffpainter(this); QPainter* painter = &ffpainter; painter->save(); //QBrush background_brush= QColor(255, 255, 255, 0); // 透明色 //painter->fillRect(this->rect(), background_brush); QColor text_color=QColor(255,255,255,0.7); // 文本顏色 QColor filter_color= QColor(250, 50, 57, 255); // 高亮顏色 // 高亮文本 QString filter_text = m_filterHover; // 文字與過濾文字首先獲取出來 QString text = m_strName; painter->setFont(this->font()); QFontMetrics font_metrics(this->font()); int text_width = this->width(); m_LabelName->setDefaultFont(this->font()); // 右部顯示省略號 QFontMetrics font_width(this->font()); text = font_width.elidedText(text, Qt::ElideRight, text_width); // 然後將匹配項加上font-color標籤,若非空的話 if (!filter_text.isEmpty()) { int last_index = text.lastIndexOf(filter_text, -1, Qt::CaseInsensitive); while (last_index != -1) { text.insert(last_index + filter_text.length(), "</font>"); text.insert(last_index, QString("<font color=%1>").arg(filter_color.name())); last_index = text.lastIndexOf(filter_text, last_index, Qt::CaseInsensitive); } } // 在文檔中設置字體,並設置文字 m_LabelName->setHtml(QString("<font color=%1>%2</font>").arg(text_color.name()).arg(text)); int y = this->height();//居中顯示高度 y = y / 2 - 10; if (y<0) { y = 0; } QRect text_rect = QRect(this->x(), y, text_width, 20); // 文字高度爲20,所以文字上下空白爲6px painter->translate(QPoint(text_rect.x(), text_rect.y() + (text_rect.height() - font_metrics.height()) / 2)); m_LabelName->drawContents(painter, text_rect.translated(-text_rect.x(), -text_rect.y())); painter->restore(); }
(3)對象的創建和調用
m_LabelName = new CollorLabel();
m_LabelName->setParent(ui.widgetName);
ui.horizontalLayoutName->addWidget(m_LabelName);
m_LabelName->SetName(info.value("name").toString());
m_LabelName->setToolTip(info.value("name").toString());