Hey there.
I'm using a QTextDocument to display paragraphes.
Is there a way to set the "default" text color.
Thanks.
Hey there.
I'm using a QTextDocument to display paragraphes.
Is there a way to set the "default" text color.
Thanks.
Qt Code:
mTextDocument->setDefaultStyleSheet(style.arg(mColor.red()). arg(mColor.green()). arg(mColor.blue()));To copy to clipboard, switch view to plain text mode
Tried this with no success.
Shouldn't it be QString("color: %1;").arg(mColor.name()) ?
Should be working with rgb :/.
What do you do with the document afterwards? Maybe it's enough to set the color in the object (widget?) used as a container for the document?
Actually it's not set in a widget,
I use the TextDocument for HTML strings that are displayed by my view's paint event.
Last edited by bunjee; 15th April 2008 at 18:04.
Why don't you add the style definition to the html manually?
I was thinking doing so. I was just checking that nobody had a better solution.
And beside I'm not a big fan of Html parsing.
You can also set a QTextFormat for each document block. Setting a pen with a colour of your choice on the painter in the paint event before painting might also help.
Unfortunately, that didn't help,
I'm still trying to figure out how I can set a default color for a QTextDocument without erasing html tags and stuff like that.
Changing the pen in the paint event has no effect.
There's got to be some convenient way to do so.
A QTextDocument is never alone. You ought to draw it in a paint function. There, you pass a context parameter. The latter might be just a QAbstractTextDocumentLayout.PaintContext(). It has a palette attribute. There, you set the brush.
That's how it might look like:
Qt Code:
class StyledItemDelegate(QStyledItemDelegate): def paint( self, painter: QPainter, option: QStyleOptionViewItem, ) -> None: self.initStyleOption(option, index) style: QStyle if option.widget: style = option.widget.style() else: doc.setHtml(option.text) option.text = "" painter.save() painter.translate(text_rect.topLeft()) painter.setClipRect(option.rect.translated(-text_rect.topLeft())) painter.translate(0, 0.5 * (option.rect.height() - doc.size().height())) doc.documentLayout().draw(painter, ctx) painter.restore()To copy to clipboard, switch view to plain text mode
Punctual as always, me.
You do realize that the post you are replying to is nearly 17 years old, right? And that the Qt document links you have provided are to Qt 4.8, two major versions ago, and that they lead to non-existent 404 pages?
Edit:
Actually, it looks like those links might have been automatically inserted by the forum, so never mind.
Last edited by d_stranz; 20th March 2025 at 00:23.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks