Results 1 to 3 of 3

Thread: Using Text/Fonts with QPainter in combination with QPainter::scale()

  1. #1
    Join Date
    Apr 2006
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using Text/Fonts with QPainter in combination with QPainter::scale()

    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).
    Bildschirmfoto2.jpg

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using Text/Fonts with QPainter in combination with QPainter::scale()

    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.

  3. #3
    Join Date
    Apr 2006
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using Text/Fonts with QPainter in combination with QPainter::scale()

    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 :-)

Similar Threads

  1. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  2. Replies: 5
    Last Post: 7th September 2009, 20:57
  3. QBitmap does not scale correctly with QPainter
    By macaco in forum Qt Programming
    Replies: 0
    Last Post: 8th August 2008, 11:40
  4. QPainter, scale(), and setFont()
    By c_07 in forum Qt Programming
    Replies: 11
    Last Post: 2nd January 2008, 19:46
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.