Results 1 to 15 of 15

Thread: bad sceneRect with scaled text

  1. #1
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question bad sceneRect with scaled text

    In a subclassed QGraphicsTextItem, I'm drawing some text and scaling it down in size to fit into a small area in the Scene. (I found that I have to scale, because the needed size is smaller than what I can get by using a small point size with setFont().) I adjust the scale as follows, depending on the maxHeight that I want to see in the scene.

    Qt Code:
    1. setFont(QFont("Times", 40));
    2. setDefaultTextColor(QColor(Qt::white));
    3. QFontMetricsF fm(font());
    4. qreal s = maxHeight / fm.height();
    5. scale(s, -s);
    6. translate(-(fm.width(net)/2), -(fm.height()/2));
    7. setPlainText(net);
    To copy to clipboard, switch view to plain text mode 
    The text is displayed properly. However, when I try zooming to fit, using the code below, the sceneRect is much, much larger than what I expect. So, the zoom doesn't work right. Without the text everything works fine. When I add the text the zoomed view shows everything crammed into a tiny area in a corner with empty space covering the rest of the view. It's almost as if the sceneRect() is being computed using unscaled, rather than scaled text.

    Qt Code:
    1. pcbDoc->getScene()->setSceneRect(QRectF());
    2. view->fitInView(pcbDoc->getScene()->sceneRect(), Qt::KeepAspectRatio);
    To copy to clipboard, switch view to plain text mode 
    Obviously I'm doing something wrong, but I need a hint.
    Thanks!
    Last edited by jpn; 8th July 2008 at 07:46. Reason: changed [indent] to [code] tags

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

    Default Re: bad sceneRect with scaled text

    Where did you put the first snippet of code you presented?

  3. #3
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad sceneRect with scaled text

    I placed it in the constructor of my subclassed QGraphicsTextItem class.

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

    Default Re: bad sceneRect with scaled text

    Could you prepare a minimal compilable application reproducing the problem?

  5. #5
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Re: bad sceneRect with scaled text

    I just hacked something into the Qt Elastic Nodes example, and it seems to work as expected - the problem doesn't occur there....

    hmm... I must have some stupid mistake in my code. I'll check again.

  6. #6
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad sceneRect with scaled text

    I think the problem is related to the fact that sceneRect() always grows in size but never shrinks, even if you set a null sceneRect(), it will return a size that is the largest the scene extents have ever been.

    For zooming to fit, I want to know the bounding rect of the current scene, not what it might have been in the past. I changed my zoom function to use itemsBoundingRect() instead and it works. This isn't my preferred solution, as it takes too long to compute. I think I will change my code to maintain my own overall bounding rect, and then use that in the zoom-to-fit function.

    I'm still not sure why the text resulted in such a large sceneRect... The sceneRect is computed using the using the transformed / scaled items, right?

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

    Default Re: bad sceneRect with scaled text

    Quote Originally Posted by koosh View Post
    I think the problem is related to the fact that sceneRect() always grows in size but never shrinks, even if you set a null sceneRect(), it will return a size that is the largest the scene extents have ever been.
    Not really. It will return the minimum size to contain all the items. If you move items closer to each other and reset the scene size, it will decrease its size.

    I'm still not sure why the text resulted in such a large sceneRect... The sceneRect is computed using the using the transformed / scaled items, right?
    No, sceneRect is sceneRect. It doesn't change regardless of the zoom. You zoom the view, not the scene. The Earth doesn't become bigger if you move closer to it - it is just your impression because you SEE it bigger.

  8. #8
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad sceneRect with scaled text

    Wysota, thanks for helping me to understand. Perhaps I'm misinterpreting the Qt documentation regarding scenerect:


    If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).

    So does it ever shrink or not?

    I'm scaling the GraphicsTextItem. So, that means the Text is stored in the scene as scaled text, not unscaled, right? I understand that zooming the view doesn't change the scene, but this is different, right? I'm talking about scaling the item in the scene, not the view. The scene Rect that is computed should be for text as it is actually stored in the scene (scaled down), regardless of the view.

    Thanks.

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

    Default Re: bad sceneRect with scaled text

    Quote Originally Posted by koosh View Post
    So does it ever shrink or not?
    Not by itself, but if you reset the size (for instance by setting it to some real value and then to an invalid one again), it will shrink.

    I'm scaling the GraphicsTextItem. So, that means the Text is stored in the scene as scaled text, not unscaled, right?
    Yes, but the sceneRect doesn't change. Only the space covered by the item on the scene changes.

    The scene Rect that is computed should be for text as it is actually stored in the scene (scaled down), regardless of the view.
    I don't really understand what you mean, but you are probably right

  10. #10
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad sceneRect with scaled text

    Quote Originally Posted by wysota View Post
    Not by itself, but if you reset the size (for instance by setting it to some real value and then to an invalid one again), it will shrink.
    Ok, I think I see.... I was expecting the sceneRect() be be managed entirely by the QGraphicsScene, but I realize that I really should be mantaining the sceneRect and calling setSceneRect( ) as I change the scene. I looked at the QT code, and there it recommends that we should always call setSceneRect() when operating on large scenes, because otherwise if we ask QGraphicsScene to compute it , it just iterates over all items on the scene calling itemsBoundingRect(). (this is time consuming and what I wanted to avoid).

    Yes, but the sceneRect doesn't change. Only the space covered by the item on the scene changes.
    This seems counter-intuitive. For example, if I create a rect of size (10,10), and then scale it by (0.1, 0.1) and add it to a scene, the sceneRect returns (10,10), even though the rect is only covering an area of (1,1). My expectation was that the sceneRect would return (1,1) because it would have applied the item's transformation.

    I don't really understand what you mean, but you are probably right
    Put another way: the item transformation (scaling) is only used when rendering the item to a graphics view. The sceneRect computation doesn't consider item transformation.

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

    Default Re: bad sceneRect with scaled text

    Quote Originally Posted by koosh View Post
    This seems counter-intuitive.
    It might be a bug.

    My expectation was that the sceneRect would return (1,1) because it would have applied the item's transformation.
    Maybe you should try switching the order of operations, maybe that helps.

  12. #12
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad sceneRect with scaled text

    Quote Originally Posted by wysota View Post
    It might be a bug.


    Maybe you should try switching the order of operations, maybe that helps.
    I tried that a few days back, and IIRC, it caused a change, but not an improvement.

  13. #13
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad sceneRect with scaled text

    So is it true that in general for large scenes, I should be managing the sceneRect() myself, and calling setSceneRect() for what I want the overall scene to be? Or is this really only necessary if I scale or otherwise transform any graphicsitems, and if not, let qgraphicsscene do it for me?

    What is the best practice regarding this?

    thx!

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

    Default Re: bad sceneRect with scaled text

    The best practice is to manage the scene size yourself.

  15. #15
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad sceneRect with scaled text

    Ok, thank you.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 14:48
  4. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.