PDA

View Full Version : Limiting text inside a QGraphicsItem's item?



qtzcute
16th August 2009, 17:44
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.


QGraphicsTextItem *text = new QGraphicsTextItem(this);
text->setTextInteractionFlags(Qt::TextEditorInteraction) ;
text->setPos(5,-245);
text->setPlainText(name);

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.

Boron
16th August 2009, 18:10
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...

scascio
14th September 2009, 14:15
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.

qtzcute
16th September 2009, 06:10
Thanks scascio and Boran. I worked it out finally. :)