Qt-interest Archive, June 2007
Rotation of QGraphicsItems
Message 1 in thread
Hi,
This may be a trivial conceptual issue, but this is smthing that is
giving me headache for a long time.
I tried reading the doc, but that didnt help much. If someone could
please clarify my doubt about QGraphicsItem rotation,
it would be of great help. Thnx!
The scenariois like this :-
I have a QGraphicsRectItem and when I rotate it, it rotates on the
top-left corner. So far so good.
Now I have a QGraphicsItem, which contains a polynomial, a circle and a
few lines (some complex shape).
If I rotate the parent widget, i expect the whole shape to rotate about
"some" point.
1. The shape is rotating. But I am not able to figure out, about which
point ?
2. On the parent QGraphicsItem pos() always returns (0,0), i do not know
why ? Should It be scene co-ordinates ?
3. If I do a moveBy(), it moves fine, but setPos(0,0) does not position
it to (0,0) in scene coordinates.
I position the Item at (0,0) and then rotate, it shifts down. Why ? The
way it shifts, it seems it is not rotating about its center also.
How does GraphicsItem rotate ?
It would be really helpful if someone could shed some light on this
concept which I seem to miss out :-(.
Here is my code :-
//CONSTRUCTING THE MAIN QGRAPHICSITEM
QRectF Block::boundingRect() const
{
return m_boundingRect;
}
Block::Block(QRectF boundingRect, QVector<QPointF>pointVec,
QVector<QRectF>ellipseVec, QVector<QPolygonF>polyVec,
QVector<QLineF>lineVec, QGraphicsItem *parent, QGraphicsScene *scene) :
m_boundingRect(boundingRect),QAbstractGraphicsShapeItem(parent, scene)
{
m_scene = scene ;
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setAcceptsHoverEvents(true);
m_pointVec = pointVec;
m_ellipseVector = ellipseVec;
m_polygonVector = polyVec ;
m_lineVector = lineVec ;
for(int i = 0 ; i < polyVec.count(); i++)
{
m_polygon = scene->addPolygon(polyVec[i]);
m_polygon->setParentItem(this);
itemVector.append(m_polygon);
}
for (int i = 0 ; i < lineVec.count(); i++)
{
m_line = scene->addLine(lineVec[i]);
m_line->setParentItem(this);
itemVector.append(m_line);
}
for (int i = 0 ; i < ellipseVec.count(); i++)
{
m_ellipse = scene->addEllipse(ellipseVec[i]);
m_ellipse->setParentItem(this);
itemVector.append(m_ellipse);
}
}
//ROTATION FROM QGRAPHICSVIEW
void MyView::rotateFigure()
{
QList<QGraphicsItem *>item = m_scene->selectedItems();
for(int i = 0; i < item.count(); i++)
{
Block * tmp1 = dynamic_cast<Block*>(item[i]);
if(tmp1)
{
QRectF rect = tmp1->boundingRect();
QPointF p(rect.x(), rect.y());
tmp1->moveBy(-(p.x()),-(p.y())); //tmp1->setPos(0,0) does not work .. I
do not understand why ?
tmp1->rotate(10);
tmp1->moveBy(p.x(), p.y());
}
}
update();
}
Thnx
Prateek
Message 2 in thread
On 6/4/07, Prateek Tiwari <ptiwari@xxxxxxxxxxx> wrote:
>
> Hi,
>
> This may be a trivial conceptual issue, but this is smthing that is giving
> me headache for a long time.
> I tried reading the doc, but that didnt help much. If someone could please
> clarify my doubt about QGraphicsItem rotation,
> it would be of great help. Thnx!
>
> The scenariois like this :-
>
> I have a QGraphicsRectItem and when I rotate it, it rotates on the
> top-left corner. So far so good.
> Now I have a QGraphicsItem, which contains a polynomial, a circle and a
> few lines (some complex shape).
> If I rotate the parent widget, i expect the whole shape to rotate about
> "some" point.
>
> 1. The shape is rotating. But I am not able to figure out, about which
> point ?
> 2. On the parent QGraphicsItem pos() always returns (0,0), i do not know
> why ? Should It be scene co-ordinates ?
> 3. If I do a moveBy(), it moves fine, but setPos(0,0) does not position it
> to (0,0) in scene coordinates.
>
> I position the Item at (0,0) and then rotate, it shifts down. Why ? The
> way it shifts, it seems it is not rotating about its center also.
> How does GraphicsItem rotate ?
> It would be really helpful if someone could shed some light on
> this concept which I seem to miss out :-(.
>
Short answer - the item always rotates (and scales) about it's origin, in
parent's coordinate system. However you could simulate rotation around
arbitrary point, by moving the origin to this point (moving object to
"minus" point), rotating, and than moving it back.
I.e. in your example with rectangle item, you can rotate is around center
point by doing something like:
item->moveBy(-item->rect()->width()/2,-item->rect()->heigh()/2);
item->rotate(angle);
item->moveBy(item->rect()->width()/2,item->rect()->heigh()/2);
To get more feeling of transformations and coordinate system, try to play a
little with "Transformations Example" included in Qt distribution - it shows
the things pretty well.
--
[ signature omitted ]
Message 3 in thread
Thanx a Pavel and Dave !
Somehow I do not seem to have "Transformations example" in my Qt
distribution :-(
I am trying to understand the GraphicsView co-ordinate system, but seems
I am missing out some basic element.
I want to place an Item at the top-left corner of my window, which
happens to be the top-left corner of QGraphicsView widget.
So effectively I want to put an item at (0,0) in the view co-ordinate.
This is how I am trying to do it but it dosent work. What am I doing
wrong here ?
void MyView::moveFigure()
{
QList<QGraphicsItem *>item = m_scene->selectedItems();
for(int i = 0; i < item.count(); i++)
{
Block * tmp1 = dynamic_cast<Block*>(item[i]); //Block is a
QGraphicsItem
if(tmp1)
{
QPoint p = this->pos(); //Origin in View
Co-ordinates
QPointF p1 = this->mapToScene(p); //Map it to scene but p1
becomes as (1,0), why ????
tmp1->setPos(p1); // put the scene co-ordinates in the item
position
}
}
}
2. What is the origin for the QGraphicsScene co-ordinate system ? Is it
the center of the scene rectangle ?
It should be the same for all items in the scene. So if I do an
operation item->setPos(100,100), all items should
go to a single point(100,100) in the scene co-ordinate and which would
map to a single unique point in the view co-ordinate system,
But all items more to different points in the view. Please help me
understand this.
Many Thanx!!!
Prateek
________________________________
From: Pavel Antokolsky aka Zigmar [mailto:zigmar@xxxxxxxxx]
Sent: Monday, June 04, 2007 8:55 PM
To: qt-interest
Subject: Re: Rotation of QGraphicsItems
On 6/4/07, Prateek Tiwari <ptiwari@xxxxxxxxxxx> wrote:
Hi,
This may be a trivial conceptual issue, but this is
smthing that is giving me headache for a long time.
I tried reading the doc, but that didnt help much. If
someone could please clarify my doubt about QGraphicsItem rotation,
it would be of great help. Thnx!
The scenariois like this :-
I have a QGraphicsRectItem and when I rotate it, it
rotates on the top-left corner. So far so good.
Now I have a QGraphicsItem, which contains a polynomial,
a circle and a few lines (some complex shape).
If I rotate the parent widget, i expect the whole shape
to rotate about "some" point.
1. The shape is rotating. But I am not able to figure
out, about which point ?
2. On the parent QGraphicsItem pos() always returns
(0,0), i do not know why ? Should It be scene co-ordinates ?
3. If I do a moveBy(), it moves fine, but setPos(0,0)
does not position it to (0,0) in scene coordinates.
I position the Item at (0,0) and then rotate, it shifts
down. Why ? The way it shifts, it seems it is not rotating about its
center also.
How does GraphicsItem rotate ?
It would be really helpful if someone could shed some
light on this concept which I seem to miss out :-(.
Short answer - the item always rotates (and scales) about it's
origin, in parent's coordinate system. However you could simulate
rotation around arbitrary point, by moving the origin to this point
(moving object to "minus" point), rotating, and than moving it back.
I.e. in your example with rectangle item, you can rotate is
around center point by doing something like:
item->moveBy(-item->rect()->width()/2,-item->rect()->heigh()/2);
item->rotate(angle);
item->moveBy(item->rect()->width()/2,item->rect()->heigh()/2);
To get more feeling of transformations and coordinate system,
try to play a little with "Transformations Example" included in Qt
distribution - it shows the things pretty well.
--
Best regards,
Zigmar
Message 4 in thread
Hi Prateek,
The "Transformations example" can be found here:
http://doc.trolltech.com/4.3/painting-transformations.html
Hope this helps,
K.
Prateek Tiwari wrote:
> Thanx a Pavel and Dave !
>
> Somehow I do not seem to have "Transformations example" in my Qt
> distribution :-(
> I am trying to understand the GraphicsView co-ordinate system, but
> seems I am missing out some basic element.
>
> I want to place an Item at the top-left corner of my window, which
> happens to be the top-left corner of QGraphicsView widget.
> So effectively I want to put an item at (0,0) in the view co-ordinate.
>
> This is how I am trying to do it but it dosent work. What am I doing
> wrong here ?
>
>
> void MyView::moveFigure()
> {
> QList<QGraphicsItem *>item = m_scene->selectedItems();
> for(int i = 0; i < item.count(); i++)
> {
> Block * tmp1 = dynamic_cast<Block*>(item[i]); //Block is a
> QGraphicsItem
> if(tmp1)
> {
> QPoint p = this->pos(); //Origin in View
> Co-ordinates
> QPointF p1 = this->mapToScene(p); //Map it to scene but
> p1 becomes as (1,0), why ????
> tmp1->setPos(p1); // put the scene co-ordinates in the
> item position
> }
> }
> }
>
>
> 2. What is the origin for the QGraphicsScene co-ordinate system ? Is
> it the center of the scene rectangle ?
> It should be the same for all items in the scene. So if I do an
> operation item->setPos(100,100), all items should
> go to a single point(100,100) in the scene co-ordinate and which would
> map to a single unique point in the view co-ordinate system,
> But all items more to different points in the view. Please help me
> understand this.
>
> Many Thanx!!!
> Prateek
>
> ------------------------------------------------------------------------
> *From:* Pavel Antokolsky aka Zigmar [mailto:zigmar@xxxxxxxxx]
> *Sent:* Monday, June 04, 2007 8:55 PM
> *To:* qt-interest
> *Subject:* Re: Rotation of QGraphicsItems
>
>
>
>
> On 6/4/07, *Prateek Tiwari* <ptiwari@xxxxxxxxxxx
> <mailto:ptiwari@xxxxxxxxxxx>> wrote:
>
> Hi,
>
> This may be a trivial conceptual issue, but this is smthing
> that is giving me headache for a long time.
> I tried reading the doc, but that didnt help much. If someone
> could please clarify my doubt about QGraphicsItem rotation,
> it would be of great help. Thnx!
>
> The scenariois like this :-
>
> I have a QGraphicsRectItem and when I rotate it, it rotates on
> the top-left corner. So far so good.
> Now I have a QGraphicsItem, which contains a polynomial, a
> circle and a few lines (some complex shape).
> If I rotate the parent widget, i expect the whole shape to
> rotate about "some" point.
>
> 1. The shape is rotating. But I am not able to figure out,
> about which point ?
> 2. On the parent QGraphicsItem pos() always returns (0,0), i
> do not know why ? Should It be scene co-ordinates ?
> 3. If I do a moveBy(), it moves fine, but setPos(0,0) does not
> position it to (0,0) in scene coordinates.
>
> I position the Item at (0,0) and then rotate, it shifts down.
> Why ? The way it shifts, it seems it is not rotating about its
> center also.
> How does GraphicsItem rotate ?
> It would be really helpful if someone could shed some light on
> this concept which I seem to miss out :-(.
>
>
> Short answer - the item always rotates (and scales) about it's
> origin, in parent's coordinate system. However you could simulate
> rotation around arbitrary point, by moving the origin to this
> point (moving object to "minus" point), rotating, and than moving
> it back.
> I.e. in your example with rectangle item, you can rotate is around
> center point by doing something like:
> item->moveBy(-item->rect()->width()/2,-item->rect()->heigh()/2);
> item->rotate(angle);
> item->moveBy(item->rect()->width()/2,item->rect()->heigh()/2);
>
> To get more feeling of transformations and coordinate system, try
> to play a little with "Transformations Example" included in Qt
> distribution - it shows the things pretty well.
>
> --
> Best regards,
> Zigmar
>
--
[ signature omitted ]