{
int correction(0);
QStyleOptionViewItemV4 myOption = option;
QPalette standard_color
= myOption.
palette;
QPalette special_color
= myOption.
palette;
myOption.textElideMode = Qt::ElideNone;
QString text
= "Teno<b>st</b>1989";
int start_pos(0), prev_pos(0), current_width(0);
std::string word;
while((start_pos=text.indexOf(start_tag, prev_pos))>=0)
{
myOption.text = text.mid(prev_pos, start_pos-prev_pos);
//draw in standard color
myOption.palette = standard_color;
myOption.rect.setLeft(current_width);
myOption.rect.setWidth(font_metric.width(myOption.text)+correction);
current_width += myOption.rect.width();
prev_pos = start_pos+start_tag.size();
start_pos=text.indexOf(end_tag, prev_pos);
myOption.text = text.mid(prev_pos, start_pos-prev_pos);
myOption.palette = special_color;
myOption.rect.setLeft(current_width);
myOption.rect.setWidth(font_metric.width(myOption.text)+correction);
current_width += myOption.rect.width();
prev_pos = start_pos+end_tag.size();
}
myOption.text = text.mid(prev_pos);
myOption.palette = standard_color;
myOption.rect.setLeft(current_width);
myOption.rect.setWidth(font_metric.width(myOption.text));
}
inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index ) const
{
int correction(0);
QStyleOptionViewItemV4 myOption = option;
QPalette standard_color = myOption.palette;
QPalette special_color = myOption.palette;
myOption.textElideMode = Qt::ElideNone;
QFontMetrics font_metric(myOption.font);
special_color.setColor(QPalette::WindowText, QColor(Qt::red));
QString text = "Teno<b>st</b>1989";
int start_pos(0), prev_pos(0), current_width(0);
std::string word;
while((start_pos=text.indexOf(start_tag, prev_pos))>=0)
{
myOption.text = text.mid(prev_pos, start_pos-prev_pos);
//draw in standard color
myOption.palette = standard_color;
myOption.rect.setLeft(current_width);
myOption.rect.setWidth(font_metric.width(myOption.text)+correction);
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
current_width += myOption.rect.width();
prev_pos = start_pos+start_tag.size();
start_pos=text.indexOf(end_tag, prev_pos);
myOption.text = text.mid(prev_pos, start_pos-prev_pos);
myOption.palette = special_color;
myOption.rect.setLeft(current_width);
myOption.rect.setWidth(font_metric.width(myOption.text)+correction);
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
current_width += myOption.rect.width();
prev_pos = start_pos+end_tag.size();
}
myOption.text = text.mid(prev_pos);
myOption.palette = standard_color;
myOption.rect.setLeft(current_width);
myOption.rect.setWidth(font_metric.width(myOption.text));
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
}
To copy to clipboard, switch view to plain text mode
Bookmarks