思路为:给QTreeWidget添加QStyledItemDelegate委托,然后重新paint.代码可以优化,有兴趣的话。注意QTreeWidgetItem的text对齐方式为Qt::AlignCenter
#include "treewidget_styledItemDelegate.h"
#include <QPainter>
#include <QApplication>
#include <QStyle>
treewidget_styledItemDelegate::treewidget_styledItemDelegate()
{
}
/*!
* \brief treewidget_styledItemDelegate::paint
* \param painter
* \param option
* \param index
*/
void treewidget_styledItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QStyleOptionViewItem itemOption = option;
initStyleOption( &itemOption,index );
if (itemOption.state & QStyle::State_HasFocus)
{
//!--- 去掉选中时候的Item的虚线框,也就是焦点
itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
}
QStyledItemDelegate::paint(painter, itemOption, index); //!-- 保持原有风格不变
paint_keyword_highlight( painter,itemOption,index );
}
/*!
* \brief treewidget_styledItemDelegate::paint_keyword_highlight
* \param painter
* \param itemOption
* \param index
*/
void treewidget_styledItemDelegate::paint_keyword_highlight(QPainter *painter, const QStyleOptionViewItem &itemOption, const QModelIndex &index) const
{
int ItemHeight = 0,ItemStringWidth = 0;
int indexFindLeft = 0;
int repaintTextWidth = 0;
painter->save();
const QWidget *m_QWidget = itemOption.widget;
QStyle *m_QStyle = m_QWidget ? m_QWidget->style() : QApplication::style(); //!--- 得到当前的style
QFontMetrics m_QFontMetrics = painter->fontMetrics(); //!--- 得到这个painter的像素
QString indexColString = ( index.model()->data(index,Qt::DisplayRole).toString() );
QRect m_QRect = m_QStyle->subElementRect( QStyle::SE_ItemViewItemText,&itemOption,m_QWidget );//!--- 得到Item的自己的Rect
QPalette::ColorRole textDisplayRole = QPalette::NoRole; //!--- 设置text的role
if (itemOption.state & QStyle::State_Selected)
{
//!--- 当选中字体的时候字体显示高亮
textDisplayRole = QPalette::HighlightedText;
}
ItemHeight = itemOption.rect.height();
painter->translate( itemOption.rect.x(),itemOption.rect.y() );
painter->setFont(QApplication::font(itemOption.widget));
painter->setPen(itemOption.palette.brush(QPalette::Text).color());
indexFindLeft = indexColString.indexOf( m_regFindKeyWords ); //!--- 得到查找字体在当前整个Item字体中的位置
int findKeyWordWidth = m_QFontMetrics.width(m_regFindKeyWords); //!--- 得到查找字体的像素宽度
int colTextWidth = m_QFontMetrics.width( itemOption.text ); //!--- 得到整个字体的像素宽度
int preFindKeyWordWidth = m_QFontMetrics.width( indexColString.mid(0,indexFindLeft) ); //!-- 得到查找字体前面的字体的像素宽度
m_QRect = m_QRect.adjusted( 0,0,ItemStringWidth,ItemHeight );
QString drawLine = m_QFontMetrics.elidedText( indexColString, Qt::ElideMiddle, m_QRect.width()); //!--- 当字体超过Item的长度时显示为省略号
//!--- 以下为绘制关键字
if( index.column() >=0){
if( indexFindLeft < 0 || m_regFindKeyWords == ""){
this->displayText(indexColString,QLocale::Chinese);
}
if( indexFindLeft >= 0 && m_regFindKeyWords != ""){
for( int i = 0 ; i < indexColString.length();++i){
if( i < indexFindLeft ){
this->displayText(drawLine.mid(0,indexFindLeft),QLocale::Chinese);
i = indexFindLeft;
}else if( i > ( indexFindLeft - 1 + m_regFindKeyWords.length() ) ){
this->displayText( drawLine.mid(indexFindLeft+m_regFindKeyWords.length(),indexColString.length()-m_regFindKeyWords.length()-indexFindLeft ),QLocale::Chinese);
i = indexColString.length();
}else{
painter->setBackground(QBrush(Qt::green));
painter->setBackgroundMode( Qt::OpaqueMode );
if( colTextWidth == findKeyWordWidth ){
m_QStyle->drawItemText( painter,QRect( (itemOption.rect.width()-findKeyWordWidth)/2,0,findKeyWordWidth,ItemHeight),
itemOption.displayAlignment ,QApplication::palette(),true, drawLine.mid( indexFindLeft,m_regFindKeyWords.length()),textDisplayRole );
}else if( colTextWidth < itemOption.rect.width() && colTextWidth != findKeyWordWidth ){
repaintTextWidth = ( (itemOption.rect.width()-colTextWidth)/2+preFindKeyWordWidth);
m_QStyle->drawItemText( painter,QRect( repaintTextWidth,0,findKeyWordWidth,ItemHeight),
itemOption.displayAlignment ,QApplication::palette(),true, drawLine.mid( indexFindLeft,m_regFindKeyWords.length()),textDisplayRole );
}else if( colTextWidth > itemOption.rect.width() ){
m_QStyle->drawItemText( painter,QRect( preFindKeyWordWidth+1,0,findKeyWordWidth,ItemHeight),
itemOption.displayAlignment ,QApplication::palette(),true, drawLine.mid( indexFindLeft,m_regFindKeyWords.length()),textDisplayRole );
}
i = indexFindLeft + m_regFindKeyWords.length();
}
}
}
}
painter->restore();
}
/*!
* \brief treewidget_styledItemDelegate::search_keyword 赋值关键字
* \param regFindKeyWords
*/
void treewidget_styledItemDelegate::search_keyword(const QString ®FindKeyWords)
{
if( m_regFindKeyWords.length() > 0 ) m_regFindKeyWords.clear();
m_regFindKeyWords = regFindKeyWords;
}
欢迎加入QQ群:259787236进行讨论。