PDA

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



been_1990
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.

Lykurg
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().

been_1990
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->setPen(QColor("black"));
painter->setFont(QFont("Arial",11,QFont::AllLowercase));
//painter->drawText(QPoint(option.rect.x() + 50, option.rect.y() + 20), o->name()); // this renders perfectly in place

QTextDocument * doc = new QTextDocument();
doc->setHtml(o->name());
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..
option.rect.width(),
30));

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