PDA

View Full Version : Getting boundingRect of Text on QGraphicsWidget without drawing ?



skainz
19th April 2007, 18:22
Hello !

I'v got the following Situation:

I use a QGraphicsView to display Items, so called ClassBoxes.

So I derived a class ClassBox from QGraphicsItem , reimplemented :: paint(...) and everything is fine.

I also have another Class LWidget which holds all the ClassBox items.

I use LWidget->addClass(ClassBox* cb) to add new classes.

Now i want to spread them equally over the width of my QGraphicsView. I know I can use setPos(), but my ClassBoxes contain Text, and without drawing this text i can't get the boundingBox of this text which is about the size of my Classbox. And when I draw it, I get the boundingBoxes, but the Classes are at the wrong positions becasue I can't set them without knowing the sizes of my ClassBoxes.

Hope someone understands my cryptic text :confused: and can help me...

TIA, Simon

pdolbey
19th April 2007, 18:57
Can't you use "QRect QFontMetrics::boundingRect ( const QString & text ) const" to get the size of the bounding box?

Pete

wysota
19th April 2007, 20:11
It's not that simple... QFontMetrics only returns data for plaintext, while the graphics item can hold rich text.
I think this should work:

QGraphicsTextItem *item = ....;
//...
QSize textSize = item->document()->documentLayout()->documentSize();
QRect rect = QRect(item->pos(), textSize);

aamer4yu
20th April 2007, 04:54
but will the document size will be valid until the window is showwn once ???

I usually use a single a shot to set the scene items based on geometries, etc. so that the correct values are used from boundingRect, document size, etc

I guess this approach is good...

wysota
20th April 2007, 06:25
but will the document size will be valid until the window is showwn once ???

I usually use a single a shot to set the scene items based on geometries, etc. so that the correct values are used from boundingRect, document size, etc

boundingRect depends on the document size, not the other way round, so I guess it should work fine.

aamer4yu
20th April 2007, 06:35
ok..thx
i had actually faced a prob with initial geometries before... so i thought the same about document size :D

wysota
20th April 2007, 06:40
No, the problems you experienced are related to the fact that widgets are laid out when they are first shown and not before. This is not the case here as the item is meant to represent the document, so knowing how big its layout is should be enough to guess the item size. Please note that you could probably just call boundingRect() and achieve the rect immediately.

skainz
20th April 2007, 10:14
It's not that simple... QFontMetrics only returns data for plaintext, while the graphics item can hold rich text.
I think this should work:

QGraphicsTextItem *item = ....;
//...
QSize textSize = item->document()->documentLayout()->documentSize();
QRect rect = QRect(item->pos(), textSize);

Yes, you are right, but I only need plaintext, no rich text or otherwise formatted text.

Thank you anyway.

skainz
20th April 2007, 10:16
Can't you use "QRect QFontMetrics::boundingRect ( const QString & text ) const" to get the size of the bounding box?

Pete

Thank you, this saved my day (and saved me nights of thinking about this problem :) )