Results 1 to 12 of 12

Thread: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsItem

  1. #1
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile How to scale an object which inherit QGrahicsItem within it's parent QGraphicsItem

    Hello,I'm a newer to Qt,These days I have to do a task ,draw graphs with a coordinate. And I use QGraphicsView Frame. First , I create a class named Coordinate which inherit from QGraphicsItem, and another class named draw2Dgraph which is also inherited from QGraphicsItem.Then I use a class name graphView which inherit from QGraphicsView.
    Class Coordinate 's main work is to draw axis , ticks, ticks numbers , in a word ,is to draw coordinate system.
    Class draw2DGraph is used to draw points or 2D graphs within this coordinate system(Class coordinate), it is a child item of Class Coordinate.
    Class graphView is used to set scene and item.
    now I want to zoom in ( or zoom out ) the graph and coordinate.So I use the QGraphicsItem::scale() function. And I find that the graph (draw2DGraph object) has been out it's parent boundingrect ,where the coordinate system (Coordinate object )has also been out range it's parent range(QGraphicsScene).
    who can tell me how to let the graph only show in the coordinate system range and how to change the coordinate system 's axis and ticks.
    How to use this with QGraphicsView Frame .
    Wish you to help me !Wish your help ! with some source code to deal with zoom out or zoom in in QGraphicsView Frame!
    Thank you very much!

  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: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    You mean you don't want to make it possible to scale the child to such size that it would become larger than its parent, right? In that case reimplement QGraphicsItem::itemChange() for your item. There you can check the newly set size and compare it with the parent's. Finally you can return the new size for your item which will be bounded to the parent size.
    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. The following user says thank you to wysota for this useful post:

    christina123y (26th February 2009)

  4. #3
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    Quote Originally Posted by wysota View Post
    You mean you don't want to make it possible to scale the child to such size that it would become larger than its parent, right? In that case reimplement QGraphicsItem::itemChange() for your item. There you can check the newly set size and compare it with the parent's. Finally you can return the new size for your item which will be bounded to the parent size.
    First , I' m sorry, I' m a newer to this forum, and I don't know where to commit my question.

    Yes,you are right, now when I use QGraphicsItem::scale or QGraphicsView::scale,that only scale the viewport ,that's not what I wanted.
    And I don't catch your mean for i don't know how to use QGraphicsItem::itemChange() to signal it's size change to it's parent item. Should I use QGraphicsItem::ItemTransformHasChanged or QGraphicsItem::ItemPositionChange
    as a parameter of QGraphicsItem::itemChange() ?
    As I'm a newer to qt, so wish your reply again!
    Thank you!
    And could you provide me with source code on this ! Thank you very much!

  5. #4
    Join Date
    Aug 2008
    Posts
    60
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    Hi christina123y,
    Can please give some code example or snippet.
    Thank you.

  6. #5
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    Have you had a look at QWT

  7. #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: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    Quote Originally Posted by christina123y View Post
    And I don't catch your mean for i don't know how to use QGraphicsItem::itemChange() to signal it's size change to it's parent item. Should I use QGraphicsItem::ItemTransformHasChanged or QGraphicsItem::ItemPositionChange
    as a parameter of QGraphicsItem::itemChange() ?
    Use ItemTransformChange so that you can return a new transformation.

    And could you provide me with source code on this ! Thank you very much!
    There is a nice example in the itemChange() documentation, only that it monitors the position and not the transformation of the 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.


  8. The following user says thank you to wysota for this useful post:

    christina123y (27th February 2009)

  9. #7
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    Quote Originally Posted by wysota View Post
    Use ItemTransformChange so that you can return a new transformation.



    There is a nice example in the itemChange() documentation, only that it monitors the position and not the transformation of the item.
    Ok, Thanks!
    I'll try my best to work out it!
    Any question countered, I'll share in!

  10. #8
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    Quote Originally Posted by wysota View Post
    You mean you don't want to make it possible to scale the child to such size that it would become larger than its parent, right? In that case reimplement QGraphicsItem::itemChange() for your item. There you can check the newly set size and compare it with the parent's. Finally you can return the new size for your item which will be bounded to the parent size.
    Help me! I do the scale as your indicate. Here is my code :
    QVariant CordHandler::itemChange(QGraphicsItemChange change,
    const QVariant &value)
    {
    QTransfrom trans =transform();//Here I get this item's transformation matrix.
    if(ItemTransformHasChanged == change)
    {
    }
    }

    Here I don't know what to do next. You tell me to check the newly set size and compare it with the parent's. But I don't know how to get the newly set size of the item.
    And I want that both the size of this item and it's parent item wouldn't change,remain the same size as their original size, when user use zoom in or zoom out ,it will only show the graphs within their original size.
    You tell me to check the newly set size and compare it with the parent's,Finally I can return the new size for your item which will be bounded to the parent size. I think this from this way ,both the size of item and it's parent bounding rect are all zoomed out. That's what no I wanted. I want just show the graph(which is zoomed in later) within the original bounding rect,the graph which is outside the orginal bounding rect will not be shown.
    Could you help! Thank you !

  11. #9
    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: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    You're using the wrong change value. I said ItemTransformChange not ItemTransformHasChanged.
    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.


  12. The following user says thank you to wysota for this useful post:

    christina123y (2nd March 2009)

  13. #10
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    Quote Originally Posted by wysota View Post
    You're using the wrong change value. I said ItemTransformChange not ItemTransformHasChanged.
    here is my code :
    Qt Code:
    1. QVariant draw2dgraph::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. if(ItemTransformChange == change && parentItem() )
    4. {
    5. QTransform trans = transform();
    6. QRectF rect = trans.mapRect(boudingRect());
    7. if(parentItem()->boundingRect().size()!=rect.size())
    8. {
    9. rect = parentItem()->boundingRect();
    10. }
    11. }
    12. return QGraphicsItem::itemChange(change,value);
    13. }
    To copy to clipboard, switch view to plain text mode 

    but the draw2DGraph's object still outside it's parent boundingRect after it zoom in.
    In draw2DGraph class I do zoom in function like this
    Qt Code:
    1. void draw2DGraph::zoomIn()
    2. {
    3. scale(1.2,1.2);
    4. }
    To copy to clipboard, switch view to plain text mode 
    why ? why still happen like this? wish you help again! Thank you
    Last edited by wysota; 6th March 2009 at 23:08. Reason: missing [code] tags

  14. #11
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    Quote Originally Posted by wysota View Post
    You're using the wrong change value. I said ItemTransformChange not ItemTransformHasChanged.
    Can you help me? This problem has troubled me almost a week, I really need help? And I have look up reference on internet, but resolvation on this problem is so little. So I really need your help! Could you please provide me with some demo or sources on this problem!Thank you very much!

  15. #12
    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: How to scale an object which inherit QGrahicsItem within it's parent QGraphicsIte

    You have to map your size to your parent's coordinate space (or vice versa) based on the transform you get as the argument. This will allow you to compare your rectangle and the rectangle of your parent. Then you'll be able to correct the matrix based on the comparison (by scaling it back down).
    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.


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.