View Full Version : Draw some characters of string in bold - QAbstractItemDelegate - QPainter

19th February 2013, 22:18
I'm using painter->drawTex() to draw text on the qlistview, but how can I make some of the string's characters be drawn in bold?
I did think about drawing each character separatly, but I can't get the width of the drawn character, and a fixed width would not be portable.

20th February 2013, 07:21
You have to use QFontMetrics to determine the width of characters or strings.

To have a more easy approach you can use QTextDocument which accepts HTML and then render the document to your painter using QTextDocument::drawContents().

21st February 2013, 16:47
Ok, it seems like it would work, but for some reason the string is writen only on first row and only when scrolling up...

//painter->drawText(QPoint(option.rect.x() + 50, option.rect.y() + 20), o->name()); // this renders perfectly in place

QTextDocument * doc = new QTextDocument();
doc->drawContents(painter,QRect(option.rect.x() + 50,
option.rect.y(), // if I use + 20, the text is never rendered, not even in first row..

My problem is similar to http://www.qtforum.org/article/37784/qtextdocument-as-item-in-qlistwidget.html, and that never got resolved.