Results 1 to 11 of 11

Thread: QGraphicsView scaling horizontally

  1. #1
    Join Date
    Oct 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView scaling horizontally

    I have a QGraphicsItem subclass that is a rectangle with text on it. I want to be able to zoom in from the view but the rectangles should only be scaled horizontally. I can't do it like this view->scale(2,1) because then the text and the edges of the QGraphicsItems will get warped. In other words, the rectangles should appear to increase in width but the text on these rectangles should stay the same size.

    To do that properly I have to modify the scene to change the width of the rectangles every time I zoom in or zoom out. This in itself is not a problem because I only have one view and it works fine but I think it diminishes the point of separating the view from the scene since zooming is something that is only related to how you view the scene. It also adds a lot of ugly code to the qgraphicsitem subclass and the qgraphicsscene subclass.


    There are other solutions like painting the text on the rectangles from the view but that is even uglier than changing the width of the rectangles.


    Does anyone know of a better solution?

    Thanks in advance.

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

    Default Re: QGraphicsView scaling horizontally

    You can set the ItemIgnoresTransformations on the text items. Then you can scale their parent (the rectangle) normally and optionally adjust the text to the new width of the rectangle. However I think that simply increasing the width of the rect is a much simpler and better idea. It's also an option to overpaint the rectangle directly on the view's viewport instead of making it an item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scaling horizontally

    Quote Originally Posted by wysota View Post
    You can set the ItemIgnoresTransformations on the text items. Then you can scale their parent (the rectangle) normally and optionally adjust the text to the new width of the rectangle. However I think that simply increasing the width of the rect is a much simpler and better idea. It's also an option to overpaint the rectangle directly on the view's viewport instead of making it an item.
    Thanks, painting on the view's viewport sounds good (would I do this from the view's paintEvent()?). But I don't see any function to get only those items that are currently visible from the view. If I'm not mistaken I would have to loop though all the items checking one by one if they are visible and this would ignore all the efficient algorithms and data structures that QGraphicsView has behind the scenes.

    If I'm mistaken and you can do it efficiently then I'll give it a try. Can you?

    Thanks again.
    Last edited by pssss; 18th March 2011 at 22:50.

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

    Default Re: QGraphicsView scaling horizontally

    Quote Originally Posted by pssss View Post
    would I do this from the view's paintEvent()?
    Yes.
    But I don't see any function to get only those items that are currently visible from the view.
    What do you need this for?

    If I'm not mistaken I would have to loop though all the items checking one by one if they are visible and this would ignore all the efficient algorithms and data structures that QGraphicsView has behind the scenes.
    If I understand that you mean you would have to paint all the items yourself then no, you wouldn't. Simply call the base class implementation of the paint event.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scaling horizontally

    Quote Originally Posted by wysota View Post
    Yes.

    What do you need this for?


    If I understand that you mean you would have to paint all the items yourself then no, you wouldn't. Simply call the base class implementation of the paint event.
    Thanks for the quick reply. I think I need to do that to follow your advice:
    "It's also an option to overpaint the rectangle directly on the view's viewport instead of making it an item."

    I have very many rectangles not just one, some wouldn't be visible since you can scroll the view. QGraphicsView only paints those which are currently visible. If I have to loop linearly over all the scene's rectangles to paint only the ones that are visible it would take O(n). But I think QGraphicsView uses some tree structure behind the scenes to make it much more efficient to check which items need to be painted, I guess it's O(log n).

    I only see the function items() in the QGraphicsView API but this one returns all the scene's items, not just the visible ones.

    I could be wrong about all the above, if I am, should I just loop over all the items from items() and paint the visible ones?

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

    Default Re: QGraphicsView scaling horizontally

    I don't really get what you mean. Why would you have to paint something that is not visible?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scaling horizontally

    That's what I don't want. I only want to paint the visible ones. But I can only get a list of all the items, regardless of whether they are visible or not. From this list I have to find out which ones are visible, to paint only those. If I have to loop through this list it would take a lot of time, O(n). I was wondering if there is a way to get a list of only those items that are visible within a QGraphicsView, so I don't have to iterate over the whole list of all items.

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

    Default Re: QGraphicsView scaling horizontally

    Quote Originally Posted by pssss View Post
    That's what I don't want. I only want to paint the visible ones. But I can only get a list of all the items, regardless of whether they are visible or not. From this list I have to find out which ones are visible, to paint only those.
    Why and where do you want to paint those items?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scaling horizontally

    Quote Originally Posted by wysota View Post
    Why and where do you want to paint those items?
    Thanks for your patience. I have QGraphicsItems which are rectangles with text on them. The paintEvent of the item itself wouldn't work because of the reasons from the first post, it wouldn't scale properly. Taking your advice I would paint them from the view however I want it, but I would still store those rectangles with text on them as QGraphicsItem, however the paintEvent function of these items wouldn't do anything since they would be painted from the view.

    in the subclass of QGraphicsItem I would have: paintEvent(QPaintEvent * event) {}

    To paint them I would go to the paintEvent() function from the subclass of QGraphicsView and paint those items from there according to the transformation that the view has.

    I could use something else instead of a QGraphicsItem subclass to store those rectangles but it's more convenient for me to store them like that. I could have a list of QRects (or subclass of QRects) stored in the scene but again I prefer to have those rectangles as QGraphicsItems, even if the paintEvent() of the items is empty.

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

    Default Re: QGraphicsView scaling horizontally

    Quote Originally Posted by pssss View Post
    Thanks for your patience. I have QGraphicsItems which are rectangles with text on them. The paintEvent of the item itself wouldn't work because of the reasons from the first post, it wouldn't scale properly. Taking your advice I would paint them from the view however I want it, but I would still store those rectangles with text on them as QGraphicsItem, however the paintEvent function of these items wouldn't do anything since they would be painted from the view.

    in the subclass of QGraphicsItem I would have: paintEvent(QPaintEvent * event) {}
    Items don't have a paintEvent. They have a paint() routine.

    To paint them I would go to the paintEvent() function from the subclass of QGraphicsView and paint those items from there according to the transformation that the view has.

    I could use something else instead of a QGraphicsItem subclass to store those rectangles but it's more convenient for me to store them like that. I could have a list of QRects (or subclass of QRects) stored in the scene but again I prefer to have those rectangles as QGraphicsItems, even if the paintEvent() of the items is empty.
    If you want to repeat all that QGraphicsView does (transformations, scrolling, clipping, etc.) only to make the text fit the item that scales in horizontal direction only then it's much better to use Graphics View to its full potential and only tweak the scaling and fitting as told at the beginning. At worst you'll have to implement your own item class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Oct 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scaling horizontally

    Ok, I'll just leave it like I have it which changes the width.

    Thanks for your help.

Similar Threads

  1. General advice about scaling on QGraphicsView/QGraphicsScene
    By onurozcelik in forum Qt Programming
    Replies: 0
    Last Post: 24th May 2010, 09:14
  2. Scaling of pen width in QGraphicsView
    By spuds in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2008, 02:47
  3. Scaling Painter without scaling the coordinate sytem
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 7th January 2008, 22:30
  4. widget qgraphicsview scaling
    By mistertoony in forum Qt Programming
    Replies: 10
    Last Post: 20th March 2007, 23:46
  5. Efficient Scaling and Rotating of QGraphicsView
    By forrestfsu in forum Qt Programming
    Replies: 10
    Last Post: 12th December 2006, 17:28

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.