I want to display parts of the text of a QTableWidgetItem
in different colors (a part of it should be displayed red).
What I found is using a QStyledItemDelegate
, reimplementing the paint
function and display a QTextDocument
that uses the the item text and added HTML.
This enables HTML for the text:
void DifferencesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
painter->save();
QTextDocument document;
document.setHtml(index.data().toString());
document.setPageSize(option.rect.size());
QAbstractTextDocumentLayout::PaintContext context;
painter->translate(option.rect.x(), option.rect.y());
document.documentLayout()->draw(painter, context);
painter->restore();
}
However, the result has some pixels offset compared to the "normal" display (which could most probably fixed somehow in a consistent way), but I wonder if there's a simpler method. I do not need HTML at all, I just want to change the color of some part of the text.
So is it possible to draw the item's text (letter by letter) and to set the color for each letter without having to use a QTextDocument
?