Results 1 to 9 of 9

Thread: Getting boundingRect of Text on QGraphicsWidget without drawing ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    1
    Qt products
    Qt4

    Question Getting boundingRect of Text on QGraphicsWidget without drawing ?

    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 and can help me...

    TIA, Simon
    Attached Images Attached Images
    Last edited by skainz; 19th April 2007 at 18:28.

  2. #2
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting boundingRect of Text on QGraphicsWidget without drawing ?

    Can't you use "QRect QFontMetrics::boundingRect ( const QString & text ) const" to get the size of the bounding box?

    Pete

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

    skainz (20th April 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting boundingRect of Text on QGraphicsWidget without drawing ?

    It's not that simple... QFontMetrics only returns data for plaintext, while the graphics item can hold rich text.
    I think this should work:
    Qt Code:
    1. QGraphicsTextItem *item = ....;
    2. //...
    3. QSize textSize = item->document()->documentLayout()->documentSize();
    4. QRect rect = QRect(item->pos(), textSize);
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting boundingRect of Text on QGraphicsWidget without drawing ?

    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...

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting boundingRect of Text on QGraphicsWidget without drawing ?

    Quote Originally Posted by aamer4yu View Post
    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.

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting boundingRect of Text on QGraphicsWidget without drawing ?

    ok..thx
    i had actually faced a prob with initial geometries before... so i thought the same about document size

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting boundingRect of Text on QGraphicsWidget without drawing ?

    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.

  9. #8
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    1
    Qt products
    Qt4

    Default Re: Getting boundingRect of Text on QGraphicsWidget without drawing ?

    Quote Originally Posted by wysota View Post
    It's not that simple... QFontMetrics only returns data for plaintext, while the graphics item can hold rich text.
    I think this should work:
    Qt Code:
    1. QGraphicsTextItem *item = ....;
    2. //...
    3. QSize textSize = item->document()->documentLayout()->documentSize();
    4. QRect rect = QRect(item->pos(), textSize);
    To copy to clipboard, switch view to plain text mode 
    Yes, you are right, but I only need plaintext, no rich text or otherwise formatted text.

    Thank you anyway.

  10. #9
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    1
    Qt products
    Qt4

    Default Re: Getting boundingRect of Text on QGraphicsWidget without drawing ?

    Quote Originally Posted by pdolbey View Post
    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 )

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 2
    Last Post: 23rd July 2012, 08:42
  3. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  4. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  5. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 09:02

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
  •  
Qt is a trademark of The Qt Company.