PDA

View Full Version : Using Text/Fonts with QPainter in combination with QPainter::scale()



Wurgl
24th December 2012, 20:58
Hello!

I would like to draw something like a timeline with ticks on it, showing timestamps. Simple said: a 1-dimenional diagram.

Now this timeline has some size, lets say 1000 pixels wide (but is resizable) and may show events within a few minutes or a couple of weeks.

Works fine with scaling the thing using QPainter::scale(this->width() / (double)minTime.secsTo(maxTime), 1) // minTime, maxTime ist of type QDateTime

So I can resize the widget, I can change the min and max time which is shown and I do not get grey hair by scaling myself at almost every line I hack in. Perfect!

Now I want to add ticks and label those ticks with the time corresponding to it's position.

The ticks themself are no problem, that works fine. But the font bothers me. That font gets scaled too, making it unreadable. If the timeline shows one week and has 1000 pixels of with, the actual scaling factor would be 0.00165 and any font would be unreadable since it would squeeze whole words into one single pixel.

Stretching the font is not an option, since the strtech factor is limited (4000 says the docu).
Scaling the font larger before drawing is not a satisfying option, since this means that I have to do my own scaling when drawing the vertical ticks.

So I end up with doing my font drawing before I use QPainter::scale() and calculation the position with my private scaling. Maybe I did not really understand viewport? Maybe I missed some flag which prevents the squeezing of the font?

I attach a screenshot of the relevant part of this prototype (yes, strings still overlap).
8528

lanz
25th December 2012, 09:22
You can take a look at the Qwt library (namely QwtScaleWidget - http://qwt.sourceforge.net/class_qwt_scale_widget.html, and customize it to your case or just to have a peek at sources).

Or you can use QGraphicsScene/QGraphicsView to do custom rendering, also
there is QGraphicsItem::ItemIgnoresTransformations for a QGraphicsItem based objects, that I used for the similar task.

IMHO it uses the same trick - not applying scale/rotate/shear transformations to these items.
So, as far as I see, your solution is OK.

Wurgl
26th December 2012, 01:03
Finally decided to solve this by handwork :-) Before applying the scaling to QPainter, I draw all the texts into the widget and do my own scaling on the position only. Works fine.

For a short moment, I looked at QGraphicsSimpleTextItem. But this seems to be a to large hammer for my tiny nail :-)