Results 1 to 4 of 4

Thread: Limiting text inside a QGraphicsItem's item?

  1. #1
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Limiting text inside a QGraphicsItem's item?

    hi geeks and helpers,

    I am trying to limit a text inside the borders of shapes, i draw in QGraphicsView. Shapes are basically items (QGraphicsItem). I set text on them by following LOCs each time i draw one.
    Qt Code:
    1. text->setTextInteractionFlags(Qt::TextEditorInteraction);
    2. text->setPos(5,-245);
    3. text->setPlainText(name);
    To copy to clipboard, switch view to plain text mode 

    where 'name' is just a QString that stores name of the shape being drawn.

    Now, if the length of the text gets greater than the width of shape, it flows out of the borders of shape as evident in the attached pics.

    Does Qt provide some quick way out of it?

    Thankyou.
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Limiting text inside a QGraphicsItem's item?

    http://doc.trolltech.com/4.5/qfontmetrics.html#width
    Look at the example in the "Detailed Description".
    With this information you should be able to limit the string length if necessary. Or you could decrease font size, or break the text to several lines or...

  3. The following user says thank you to Boron for this useful post:

    qtzcute (17th August 2009)

  4. #3
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting text inside a QGraphicsItem's item?

    You have to check the width() of your QFontMetrics instance initialized with a QFont .

    I used it to write over a QGraphicsItem on a single line.
    But in ordrer to find the right size of the font, I need to loop and check by changing the font size with setPixelSize until it fits my boundaries rect size.

    I didn't find any method in the documentation to get the right size from a pixel size direclty.


    Be aware that if your item is scaled, the label you are writing will be scale too.

  5. The following user says thank you to scascio for this useful post:

    qtzcute (16th September 2009)

  6. #4
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Limiting text inside a QGraphicsItem's item?

    Thanks scascio and Boran. I worked it out finally.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. QTreeWidget get a text from child item
    By vcp in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 18:36
  3. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 16:30
  4. Can't change the text display of a QTreeview item.
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2006, 02:03
  5. Replies: 1
    Last Post: 17th March 2006, 09:19

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.