Results 1 to 2 of 2

Thread: Preventing font scale in QGraphicsItem

  1. #1
    Join Date
    Feb 2015
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question Preventing font scale in QGraphicsItem

    I am using QGraphicsTextItem to paint the text on the scene. Text is painted along the path (QGraphicsPathItem), wich is parent of my QGraphicsTextItem - so the text rotation is changed to be along the path element and is sticked to it while zooming the view. But the font size of QGraphicsTextItem is also changing while zooming the view - this is what I am trying to avoid. If I set QGraphicsItem::ItemIgnoresTransformations flag to the QGraphicsTextItem it stops rotating while it's parent (QGraphicsPathItem) does.

    zooming-font.png

    I do understand that I have to re-implement QGraphicsTextItem::paint function, but I am stuck with the coordination system. Here is the code (Label class inherits public QGraphicsTextItem):

    Qt Code:
    1. void Label::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
    2. {
    3. // Store current position and rotation
    4. QPointF position = pos();
    5. qreal angle = rotation();
    6.  
    7. // Store current transformation matrix
    8. QTransform transform = painter->worldTransform();
    9.  
    10. // Reset painter transformation
    11. painter->setTransform( QTransform() );
    12.  
    13. // Rotate painter to the stored angle
    14. painter->rotate( angle );
    15.  
    16. // Draw the text
    17. painter->drawText( mapToScene( position ), toPlainText() );
    18.  
    19. // Restore transformation matrix
    20. painter->setTransform( transform );
    21. }
    To copy to clipboard, switch view to plain text mode 

    The position (and rotation) of my text on the screen is unpredictable :( What am I doing wrong? Thank you very much in advance.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Preventing font scale in QGraphicsItem

    Try putting the QGraphicsTextItem inside of a QGraphicsRectItem. Set ItemIgnoresTranformations on the text, but not on the rectangle. Set the rectangle size to match the bounds of your path. Rotate and scale the rectangle, instead of the text.

    You can set the rectangle to have no pen and no brush, which will basically make it invisible.

Similar Threads

  1. Replies: 2
    Last Post: 4th December 2014, 12:23
  2. how to get font width depending on its scale
    By iceman in forum Qt Programming
    Replies: 0
    Last Post: 28th July 2010, 06:50
  3. QGraphicsItem scale problem
    By qlands in forum Qt Programming
    Replies: 5
    Last Post: 9th June 2010, 16:43
  4. Scale independent QGraphicsItem
    By Twoslick in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2010, 05:59
  5. QGraphicsItem::scale() bug or not ?
    By Gopala Krishna in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2007, 15:44

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.