How to change the font and color of QString?
-
Hi,
What is the exact error you get ?
Where exactly do you want that color to appear ? -
In that case, you should check the various item data roles of Qt's item view system especially ForegroundRole.
-
@learnist
I have already told you why you get an error in your code. Why bother if you're going to ignore it? You should read up how to put"
character into C++ string literals, you need to know how to do that to code successfully.Once you have done that proceed the way you were intending. That will allow you to mix colors in a
QListWidgetItem
. -
For the special rendering you want, you will have to implement a QStyledItemDelegate.
-
@SGaist
I do not see why he needs to do as you say?! :) SinceQListWidgetItem
does accept HTML, what's wrong with just using that? I don't see the need for ForegroundRole or for a delegate? Remmeber he wants certain characters with different color/weight, easy to do in HTML. -
@JonB said in How to change the font and color of QString?:
@SGaist
I do not see why he needs to do as you say?! :) SinceQListWidgetItem
does accept HTML, what's wrong with just using that? I don't see the need for ForegroundRole or for a delegate?ForegroundRole to change the color of the text but I forgot it was only for a small portion of it.
I
don'tdidn't remember QListWidgetItem supporting html out of the box, that's why I suggested the delegate. -
@SGaist said in How to change the font and color of QString?:
I don't remember QListWidgetItem supporting html out of the box, that's why I suggested the delegate.
You could use html/css-likes styles, i.e just wrap your text inside tags:
item.setData( QtCore.Qt.UserRole, "<b>{0}</b>".format('data to store for this QListWidgetItem'))
and others. Assuming it does, it will be a lot simpler for the OP than writing a delgate.
Whoops, OK, I see that was
UserRole
... One person said it did work, one said it did not? OK, maybeQListWidgetItem
does not support rich text :(