Results 1 to 8 of 8

Thread: QGraphicsView: determining the current scale factor

  1. #1
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default QGraphicsView: determining the current scale factor

    I have a QGraphicsView where I'm allowing the user to change the zoom using CTRL-MouseWheel. The way QGraphicsView::scale(float, float) is setup it requires you to store the previous scale.

    Ex:
    Qt Code:
    1. def wheelEvent(self, evt):
    2. mods = evt.modifiers()
    3. if mods == Qt.ControlModifier and evt.orientation() == Qt.Vertical:
    4. # Most mouse types work in steps of 15 degrees
    5. # in which case the delta value is a multiple of 120;
    6. # i.e., 120 units * 1/8 = 15 degrees
    7. numDegrees = evt.delta() / 8
    8. numSteps = numDegrees / 15
    9.  
    10. newScale = self._scale + ((5*numSteps)/100.0)
    11. if newScale > 0.0 and newScale < 5.0:
    12. self.resetMatrix()
    13. self.scale(newScale, newScale)
    14. self._scale = newScale
    15. return
    16. QtGui.QGraphicsView.wheelEvent(self, evt)
    To copy to clipboard, switch view to plain text mode 

    First off... is there a better way of doing this?

    This all falls apart because it relies on the fact that "self._scale" represents the current scale factor applied to the matrix. As well as a myriad of other issues that are caused by calling resetMatrix().

    So I was wondering if there is a way to derive the current scale factor. I assumed there isn't since its not part of the public API... so I'm hoping someone has come up with a smarter way to zoom in/out.

    By the way this works fine if I just want to zoom in/out but I'm also doing a fitToSelection() call which messes up everything.

    Full code is available here in case anyone is interested:
    http://www.qtcentre.org/forum/f-qt-p...iew-15739.html

  2. #2
    Join Date
    Nov 2007
    Posts
    26
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView: determining the current scale factor

    You can calculate it from the view rect by comparing the rect in view and scene coordinates

  3. #3
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: QGraphicsView: determining the current scale factor

    is it really that easy? Seems like they should have an accessor for this. Either way, thanks for the tip

  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: determining the current scale factor

    You should save the (relative) scale factor as a separate local variable in the item. Trying to recalculate it from the view or from the matrix itself will work only in some of the cases.
    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
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: QGraphicsView: determining the current scale factor

    Thanks for the reply, but does that help when calling the fitInView method?

    I have a fitSelectedItems method that will fit the view to the currently selected items which alters the transformation matrix out of my control. I could override the method and put my own logic in place but that kind of defeats the purpose of using the builtin method.

    In my case I'm not doing any shearing or rotating so it might be sufficient to do what 'robertson1' suggested (unless you can think of a reason why thats not true).

  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: determining the current scale factor

    I'm not sure what you mean. FitInView can take a QGraphicsItem pointer so you can make sure the whole item is visible without knowing its transform. And if you need to know the rectangle occupied by any item, ask it for its QGraphicsItem::sceneBoundingRect() (or for a united bounding rect for a bunch of items you find interesting). Neither of these has much to do with the "scale" of the item. If the item has a "50%" scale but its parent has a "50%" scale as well, then the sceneBoundingRect() size will be 25% of the boundingRect() size although the scale of the item is 50%. So what you want is either a so called "level of detail" which you can transform by dividing the rectangle in the view occupied by the item by the size of its bounding rect (25% in our example) or a relative scale of the item (50%) which you should keep in the item itself. Also remember that rotating an item influences its sceneBoundingRect() but not its relative scale.
    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
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: QGraphicsView: determining the current scale factor

    Thats a good point the painter does have a level of detail property that could be used for this.

    My situation is rather simple since I'm just scaling then entire view, not individual items.

    So when I'm using fitToView it scales the view to the sceneBoundingRect() of the item. The kicker is then if I want to zoom in or out from that point I have to start with a relative scale factor. Thats where the level of detail comes in play.

    I also just took a peek at the QGraphicsView source and this is peppered throughout the code:
    Qt Code:
    1. // Find the ideal x / y scaling ratio to fit \a source into \a target.
    2. qreal xratio = targetRect.width() / sourceRect.width();
    3. qreal yratio = targetRect.height() / sourceRect.height();
    4.  
    5. // Scale according to the aspect ratio mode.
    6. switch (aspectRatioMode) {
    7. case Qt::KeepAspectRatio:
    8. xratio = yratio = qMin(xratio, yratio);
    9. break;
    10. case Qt::KeepAspectRatioByExpanding:
    11. xratio = yratio = qMax(xratio, yratio);
    12. break;
    13. case Qt::IgnoreAspectRatio:
    14. break;
    15. ...
    16. painterMatrix *= QTransform()
    17. .translate(targetRect.left(), targetRect.top())
    18. .scale(xratio, yratio)
    19. .translate(-sourceRect.left(), -sourceRect.top());
    20. }
    To copy to clipboard, switch view to plain text mode 

  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: determining the current scale factor

    Quote Originally Posted by chezifresh View Post
    The kicker is then if I want to zoom in or out from that point I have to start with a relative scale factor. Thats where the level of detail comes in play.
    This one you can keep yourself in the view and update it every time you zoom the view. You don't need to know the transforms of individual 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.


Similar Threads

  1. QGraphicsView Current Center
    By arjunasd in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2010, 20:49
  2. Can I get current rotation of a QGraphicsView?
    By bjh in forum Qt Programming
    Replies: 5
    Last Post: 2nd October 2008, 22:04
  3. Problem determining size of QGraphicsView
    By Bocki in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2008, 14:54
  4. Distributing QT application for Mac OS
    By mb0 in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2007, 18:59
  5. Replies: 15
    Last Post: 21st April 2007, 17:46

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.