PDA

View Full Version : How to scale an object which inherit QGrahicsItem within it's parent QGraphicsItem



christina123y
26th February 2009, 07:04
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!

wysota
26th February 2009, 08:33
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.

christina123y
26th February 2009, 08:57
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!

h123
26th February 2009, 12:52
Hi christina123y,
Can please give some code example or snippet.
Thank you.

spud
26th February 2009, 14:01
Have you had a look at QWT (http://qwt.sourceforge.net/)

wysota
26th February 2009, 21:22
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.

christina123y
27th February 2009, 01:12
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!

christina123y
2nd March 2009, 01:55
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 !

wysota
2nd March 2009, 08:35
You're using the wrong change value. I said ItemTransformChange not ItemTransformHasChanged.

christina123y
2nd March 2009, 11:09
You're using the wrong change value. I said ItemTransformChange not ItemTransformHasChanged.

here is my code :

QVariant draw2dgraph::itemChange(GraphicsItemChange change, const QVariant &value)
{
if(ItemTransformChange == change && parentItem() )
{
QTransform trans = transform();
QRectF rect = trans.mapRect(boudingRect());
if(parentItem()->boundingRect().size()!=rect.size())
{
rect = parentItem()->boundingRect();
}
}
return QGraphicsItem::itemChange(change,value);
}

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

void draw2DGraph::zoomIn()
{
scale(1.2,1.2);
}
why ? why still happen like this? wish you help again! Thank you

christina123y
4th March 2009, 06:07
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!

wysota
6th March 2009, 22:15
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).