PDA

View Full Version : Text drawing with QPainter



Rambobino
1st November 2010, 16:10
Hi,

I have a specific need: I'm drawing text in some custom box items in a QGraphicsView. The text is created from many string and I want to have individual control over those strings. e.g.: Change the color of a string that composes the whole string:

Angle: 123° - xxx <- I want to set the value in red for example

So at first, i've created a function that glues the strings together and I'm able to manage them. But today, I've found about QPainter::drawText() and it's ability to wrap the text using a bounding rect. So all my custom code can be reproduced by a couple of lines using drawText().

The problem I get is that drawText() does not support text formatting like HTML. So I've found out about QTextDocument::drawContents which supports HTML and pretty much all the same alignment options, but now the text is managed as a text document so I don't have any control over the vertical alignment of the text with the document's page. I want the vertical alignment to be related to the whole bounding rect instead of a single text line.

So my question: Is there a way to draw some text at any position within a bounding rect and having Qt automatically manage the alignment and word wrap, but at the same time being able to put some text formatting?

Thanks!

tbscope
1st November 2010, 16:13
You can use a QTextDocument

Or, did you know you can just add QLabels too?

Rambobino
1st November 2010, 17:26
You can use a QTextDocument

Or, did you know you can just add QLabels too?

Hi tbscore, thanks for the reply.

As I said in my first post, I'm able to do almost what I want with QTextDocument, but the problem is with the vertical align. I don't want to manually add line feeds before the text so it is displayed at the vertical center of the box for example. I would like the box to be considered a big area instead of lines.

Is there a way to speficy the height of a QTextDocument line so the vertical alignment setting would actually work?

I'll look into QLabels too, thanks for the tip!