Results 1 to 3 of 3

Thread: QGraphicsItem scaling behaviour?

  1. #1
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default QGraphicsItem scaling behaviour?

    Hey,

    I'm having some strange behaviour while trying to scale a custom QGraphicsItem. Basically, I want to scale my item (its a simple QPainterPath thats shaped like a donut). I reimplemented the shape() function to make sure the shape as opposed to the bounding rectangle is the input sensitive region.

    The item is centered at (0,0) in my scene. I want the user to click on the item, and drag towards (0,0) to scale down, and away (0,0) to scale up. Since my shape is a donut, this amounts to dragging towards or away from the center of the shape. I implemented this by overloading 'mouseMoveEvent' and getting mouse x,y coordinates to calculate the distance from (0,0), and then based on that, set the scale of the object.

    So if I just calculate the distance, and move my mouse back and forth, this works as expected... my distance smoothly increases and decreases as I move the mouse around. If I actually scale the shape while moving the mouse though, it jumps back and forth (the distance), pixel to pixel... causing the scale to jump around as well... I can't figure out why.

    I'd appreciate any help figuring out what's going on!

    Qt Code:
    1. void Ring::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. double lkmx = event->pos().x();
    4. double lkmy = event->pos().y();
    5. double cen_dist = sqrt(pow(lkmx,2) + pow(lkmy,2));
    6. qDebug() << "% distance from center: " << cen_dist/_radtyp << "\n";
    7.  
    8. // so if I uncomment the lines below qDebug()'s output will jump around, otherwise it'll be
    9. // as expected
    10. //_zoomfactor = cen_dist/_radtyp;
    11. // this->setTransform(QTransform::fromScale(_zoomfactor, _zoomfactor), false);
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default Re: QGraphicsItem scaling behaviour?

    To anyone else having this issue, it was fixed by replacing
    Qt Code:
    1. event->pos().x();
    To copy to clipboard, switch view to plain text mode 

    With
    Qt Code:
    1. event->scenePos().x();
    To copy to clipboard, switch view to plain text mode 

    I guess during the scaling operation, the coordinates of the GraphicsItem were incorrect. I'd still like to understand what was going on if anyone has some insight, but there''s a band-aid fix.

  3. #3
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem scaling behaviour?

    Haven't test this out, but from the docs - "event->pos(), Returns the mouse cursor position in item coordinates." If I'm interpreting that correctly then the position is relative to the item's current scale factor.

    If that is correct then, simply taking the inverse of the item's scale and multiplying it by the event->pos, should give you the zoom factor relative to 1:1. I'll be diving into QGraphicsScene in a week or so myself, so will be sure to test this out too.

    BTW, what is the variable _radtype?

    Qt Code:
    1. void Ring::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
    2. {
    3. double dZoomFactor = QVector2d(event->pos()).length() * (1.0 / this->scale());
    4. this->setScale(dZoomFactor);
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Disable default tab behaviour for a QGraphicsItem
    By nmather in forum Qt Programming
    Replies: 3
    Last Post: 13th December 2017, 10:30
  2. Replies: 2
    Last Post: 25th March 2011, 09:18
  3. Replies: 7
    Last Post: 29th November 2010, 19:20
  4. Scale size of QGraphicsItem without scaling pen width
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2009, 00:18
  5. Scaling Painter without scaling the coordinate sytem
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 7th January 2008, 21: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.