Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, June 2007
Problem with rotation of QGraphicsItemGroup


Message 1 in thread

Hi ,
 
I have a QGraphicsItemGroup. From wht i understood from the
documentation, 
I think, if I apply rotation to the group, all the members of the group
should rotate by the same angle.
 
But, in my case the elements of the group disapear from the scene. I am
not able to understand why ?
Do I need to setup some "matrix" using setmatrix or smthg ?
 
This is what I am doing...
 
 
Block * tmp1 = dynamic_cast<Block*>(item[i]);    //Block is a derived
class of QGraphicsItemGroup and item is a vector of QGraphicsItem.
tmp1->rotate(90);
 
When I do this, the group does not rotate, but disapears from the scene.
 
Can anyone advise what is going on here ?
 
Thnx in advance!
Prateek
 

Message 2 in thread

Prateek Tiwari wrote:
> I have a QGraphicsItemGroup. From wht i understood from the 
> documentation,
> I think, if I apply rotation to the group, all the members of the 
> group should rotate by the same angle.

About what point are you rotating? The origin (0,0). Perhaps you need to 
translate the Block to the origin before calling rotate() and then 
translate back?

--Dave

--
 [ signature omitted ] 

Message 3 in thread

Thanx Dave!
But when I rotate a QGraphicsRectItem, it rotates about the top-left
corner of the rectangle,
which made me believe it rotates about the top-left corner of the
bounding box.

Is there a way to rotate the Items about its center, or any other point
except (0,0) ?

Thnx
Prateek
 

-----Original Message-----
From: Dave Smith [mailto:dave@xxxxxxxxxxxxxxx] 
Sent: Monday, June 04, 2007 9:54 AM
To: qt-interest@xxxxxxxxxxxxx
Cc: qt-interest@xxxxxxxxxxxxx
Subject: Re: Problem with rotation of QGraphicsItemGroup

Prateek Tiwari wrote:
> I have a QGraphicsItemGroup. From wht i understood from the 
> documentation, I think, if I apply rotation to the group, all the 
> members of the group should rotate by the same angle.

About what point are you rotating? The origin (0,0). Perhaps you need to
translate the Block to the origin before calling rotate() and then
translate back?

--Dave

--
 [ signature omitted ] 

Message 4 in thread

Prateek Tiwari wrote:
> Thanx Dave!
> But when I rotate a QGraphicsRectItem, it rotates about the top-left
> corner of the rectangle,
> which made me believe it rotates about the top-left corner of the
> bounding box.
>
> Is there a way to rotate the Items about its center, or any other point
> except (0,0) ?
>   

Would this work:

    qreal origX = block->x();
    qreal origY = block->y();

    block->translate( -block->width()2/, -block->height()/2 );
    block->rotate( 45 )
    block->translate( origX, origY );

That'll put the center of the block at (0,0), and then apply the 
rotation from there. origX and origY should be the original position of 
the block. width() and height() might not be available -- you might have 
to do block->boundingRect().width() or something like that.


--Dave

--
 [ signature omitted ] 

Message 5 in thread

Hi Dave,

Even rotation about (0,0) does not help. I still get the same result :-(

--Prateek 

-----Original Message-----
From: Dave Smith [mailto:dave@xxxxxxxxxxxxxxx] 
Sent: Monday, June 04, 2007 9:54 AM
To: qt-interest@xxxxxxxxxxxxx
Cc: qt-interest@xxxxxxxxxxxxx
Subject: Re: Problem with rotation of QGraphicsItemGroup

Prateek Tiwari wrote:
> I have a QGraphicsItemGroup. From wht i understood from the 
> documentation, I think, if I apply rotation to the group, all the 
> members of the group should rotate by the same angle.

About what point are you rotating? The origin (0,0). Perhaps you need to
translate the Block to the origin before calling rotate() and then
translate back?

--Dave

--
 [ signature omitted ] 

Message 6 in thread

 
Thnx for the post Dave and Kavindra. 
Intrestingly, 

qreal origX = block->x();
qreal origY = block->y();

origX and origY always are returned as 0. And it seems that the object
is rotating about the View origin and also about its origin, though I am
not sure. 

I am sending a small compilable application that showcases the issue.
On executing the application you would see two items which are created
with same co-ordinates and one is rotated.
Would someone please explain how is this rotation in the mentioned case
happening and about which point.

I have attached an the image which showes the result on executing the
application.

Many Thanx for your time!!

Prateek


HEADER 1 (block.h)
=========
#pragma once

#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPainter>
#include <QGraphicsItem>
#include <QStyleOptionGraphicsItem>


class Block : public QGraphicsItem
{
	QGraphicsScene *m_scene;
	QRectF m_boundingRect;

public:
	Block(QRectF, QGraphicsItem * parent = 0, QGraphicsScene * scene
= 0 );
	void paint(QPainter *painter, const QStyleOptionGraphicsItem
*option, QWidget *widget = 0) ;
	QRectF boundingRect() const;
	
};


IMPLEMENTATION
===============
#include "block.h"

//Block implementation

QRectF Block::boundingRect() const
{
	return m_boundingRect;
}

Block::Block(QRectF boundingRect, QGraphicsItem *parent, QGraphicsScene
*scene) : m_boundingRect(boundingRect),QGraphicsItem(parent, scene) {
	m_scene = scene ;
	setFlag(QGraphicsItem::ItemIsMovable);
	setFlag(QGraphicsItem::ItemIsSelectable);
}

void Block::paint(QPainter *painter, const QStyleOptionGraphicsItem
*option, QWidget *widget) {
	painter->setPen(QPen(option->palette.text(), 1.0,
Qt::DashLine));
	painter->setBrush(Qt::NoBrush);
	painter->drawRect(boundingRect().adjusted(2, 2, -2, -2)); }



MAIN
====
#include <qapplication.h>
#include "block.h"

int main(int argc, char** argv)
{
	QApplication app(argc,argv);
	QGraphicsScene *scene = new QGraphicsScene();
	scene->setSceneRect(QRectF(0,0,500,500));
	Block *rect = new Block(QRectF(100,100,100,100), 0, scene);
	Block *rectRotate = new Block(QRectF(100,100,100,100), 0,
scene);
	QGraphicsView m_view(scene);
	m_view.show();
	rectRotate->rotate(30);
	return app.exec();
}






















-----Original Message-----
From: Dave Smith [mailto:dave@xxxxxxxxxxxxxxx] 
Sent: Monday, June 04, 2007 7:58 PM
To: qt-interest@xxxxxxxxxxxxx
Cc: qt-interest@xxxxxxxxxxxxx
Subject: Re: Problem with rotation of QGraphicsItemGroup

Prateek Tiwari wrote:
> Thanx Dave!
> But when I rotate a QGraphicsRectItem, it rotates about the top-left 
> corner of the rectangle, which made me believe it rotates about the 
> top-left corner of the bounding box.
>
> Is there a way to rotate the Items about its center, or any other 
> point except (0,0) ?
>   

Would this work:

    qreal origX = block->x();
    qreal origY = block->y();

    block->translate( -block->width()2/, -block->height()/2 );
    block->rotate( 45 )
    block->translate( origX, origY );

That'll put the center of the block at (0,0), and then apply the
rotation from there. origX and origY should be the original position of
the block. width() and height() might not be available -- you might have
to do block->boundingRect().width() or something like that.


--Dave
------------------------------------------------------------------------
-




Attachment:

Attachment: rotation.JPG
Description: rotation.JPG


Message 7 in thread

Hi Prateek,
I'm not sure if this will help but I remember you asking about the 
coordinates.
There's documentation on that here: 
http://doc.trolltech.com/4.3/coordsys.html

Prateek Tiwari wrote:
>  
> Thnx for the post Dave and Kavindra. 
> Intrestingly, 
>
> qreal origX = block->x();
> qreal origY = block->y();
>
> origX and origY always are returned as 0. And it seems that the object
> is rotating about the View origin and also about its origin, though I am
> not sure. 
>
> I am sending a small compilable application that showcases the issue.
> On executing the application you would see two items which are created
> with same co-ordinates and one is rotated.
> Would someone please explain how is this rotation in the mentioned case
> happening and about which point.
>
> I have attached an the image which showes the result on executing the
> application.
>
> Many Thanx for your time!!
>
> Prateek
>
>
> HEADER 1 (block.h)
> =========
> #pragma once
>
> #include <QGraphicsScene>
> #include <QGraphicsView>
> #include <QPainter>
> #include <QGraphicsItem>
> #include <QStyleOptionGraphicsItem>
>
>
> class Block : public QGraphicsItem
> {
> 	QGraphicsScene *m_scene;
> 	QRectF m_boundingRect;
>
> public:
> 	Block(QRectF, QGraphicsItem * parent = 0, QGraphicsScene * scene
> = 0 );
> 	void paint(QPainter *painter, const QStyleOptionGraphicsItem
> *option, QWidget *widget = 0) ;
> 	QRectF boundingRect() const;
> 	
> };
>
>
> IMPLEMENTATION
> ===============
> #include "block.h"
>
> //Block implementation
>
> QRectF Block::boundingRect() const
> {
> 	return m_boundingRect;
> }
>
> Block::Block(QRectF boundingRect, QGraphicsItem *parent, QGraphicsScene
> *scene) : m_boundingRect(boundingRect),QGraphicsItem(parent, scene) {
> 	m_scene = scene ;
> 	setFlag(QGraphicsItem::ItemIsMovable);
> 	setFlag(QGraphicsItem::ItemIsSelectable);
> }
>
> void Block::paint(QPainter *painter, const QStyleOptionGraphicsItem
> *option, QWidget *widget) {
> 	painter->setPen(QPen(option->palette.text(), 1.0,
> Qt::DashLine));
> 	painter->setBrush(Qt::NoBrush);
> 	painter->drawRect(boundingRect().adjusted(2, 2, -2, -2)); }
>
>
>
> MAIN
> ====
> #include <qapplication.h>
> #include "block.h"
>
> int main(int argc, char** argv)
> {
> 	QApplication app(argc,argv);
> 	QGraphicsScene *scene = new QGraphicsScene();
> 	scene->setSceneRect(QRectF(0,0,500,500));
> 	Block *rect = new Block(QRectF(100,100,100,100), 0, scene);
> 	Block *rectRotate = new Block(QRectF(100,100,100,100), 0,
> scene);
> 	QGraphicsView m_view(scene);
> 	m_view.show();
> 	rectRotate->rotate(30);
> 	return app.exec();
> }
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> -----Original Message-----
> From: Dave Smith [mailto:dave@xxxxxxxxxxxxxxx] 
> Sent: Monday, June 04, 2007 7:58 PM
> To: qt-interest@xxxxxxxxxxxxx
> Cc: qt-interest@xxxxxxxxxxxxx
> Subject: Re: Problem with rotation of QGraphicsItemGroup
>
> Prateek Tiwari wrote:
>   
>> Thanx Dave!
>> But when I rotate a QGraphicsRectItem, it rotates about the top-left 
>> corner of the rectangle, which made me believe it rotates about the 
>> top-left corner of the bounding box.
>>
>> Is there a way to rotate the Items about its center, or any other 
>> point except (0,0) ?
>>   
>>     
>
> Would this work:
>
>     qreal origX = block->x();
>     qreal origY = block->y();
>
>     block->translate( -block->width()2/, -block->height()/2 );
>     block->rotate( 45 )
>     block->translate( origX, origY );
>
> That'll put the center of the block at (0,0), and then apply the
> rotation from there. origX and origY should be the original position of
> the block. width() and height() might not be available -- you might have
> to do block->boundingRect().width() or something like that.
>
>
> --Dave
> ------------------------------------------------------------------------
> -
>
>
>
>
>   
>
> ------------------------------------------------------------------------
>

-- 
 [ signature omitted ] 

Message 8 in thread

Thanx Kavindra,
I have read and I think I understand the docs on co-ordinates and
related topics.
But the actual behaviour seems not to follow the docs.
If you would try out the sample application, you would see something
strange is happening.

This is a showstopper for me for unless I get this right, I cannot
proceed in my project.
This thing is affecting scaling too.

I request all the QT expert guys here to please have a look at my sample
compilable application
below and please point out the fallacy in my approach. The problem
persistes even if you remove my
custom Item(Block) and replace it with QGraphicsRectItem.

Thnx a ton! 
Prateek

-----Original Message-----
From: Kavindra Palaraja [mailto:kdpalara@xxxxxxxxxxxxx] 
Sent: Tuesday, June 05, 2007 6:00 PM
To: Qt-interest; Prateek Tiwari
Subject: Re: Problem with rotation of QGraphicsItemGroup

Hi Prateek,
I'm not sure if this will help but I remember you asking about the
coordinates.
There's documentation on that here: 
http://doc.trolltech.com/4.3/coordsys.html

Prateek Tiwari wrote:
>  
> Thnx for the post Dave and Kavindra. 
> Intrestingly,
>
> qreal origX = block->x();
> qreal origY = block->y();
>
> origX and origY always are returned as 0. And it seems that the object

> is rotating about the View origin and also about its origin, though I 
> am not sure.
>
> I am sending a small compilable application that showcases the issue.
> On executing the application you would see two items which are created

> with same co-ordinates and one is rotated.
> Would someone please explain how is this rotation in the mentioned 
> case happening and about which point.
>
> I have attached an the image which showes the result on executing the 
> application.
>
> Many Thanx for your time!!
>
> Prateek
>
>
> HEADER 1 (block.h)
> =========
> #pragma once
>
> #include <QGraphicsScene>
> #include <QGraphicsView>
> #include <QPainter>
> #include <QGraphicsItem>
> #include <QStyleOptionGraphicsItem>
>
>
> class Block : public QGraphicsItem
> {
> 	QGraphicsScene *m_scene;
> 	QRectF m_boundingRect;
>
> public:
> 	Block(QRectF, QGraphicsItem * parent = 0, QGraphicsScene * scene
= 0 
> );
> 	void paint(QPainter *painter, const QStyleOptionGraphicsItem
*option, 
> QWidget *widget = 0) ;
> 	QRectF boundingRect() const;
> 	
> };
>
>
> IMPLEMENTATION
> ===============
> #include "block.h"
>
> //Block implementation
>
> QRectF Block::boundingRect() const
> {
> 	return m_boundingRect;
> }
>
> Block::Block(QRectF boundingRect, QGraphicsItem *parent, 
> QGraphicsScene
> *scene) : m_boundingRect(boundingRect),QGraphicsItem(parent, scene) {
> 	m_scene = scene ;
> 	setFlag(QGraphicsItem::ItemIsMovable);
> 	setFlag(QGraphicsItem::ItemIsSelectable);
> }
>
> void Block::paint(QPainter *painter, const QStyleOptionGraphicsItem 
> *option, QWidget *widget) {
> 	painter->setPen(QPen(option->palette.text(), 1.0,
Qt::DashLine));
> 	painter->setBrush(Qt::NoBrush);
> 	painter->drawRect(boundingRect().adjusted(2, 2, -2, -2)); }
>
>
>
> MAIN
> ====
> #include <qapplication.h>
> #include "block.h"
>
> int main(int argc, char** argv)
> {
> 	QApplication app(argc,argv);
> 	QGraphicsScene *scene = new QGraphicsScene();
> 	scene->setSceneRect(QRectF(0,0,500,500));
> 	Block *rect = new Block(QRectF(100,100,100,100), 0, scene);
> 	Block *rectRotate = new Block(QRectF(100,100,100,100), 0,
scene);
> 	QGraphicsView m_view(scene);
> 	m_view.show();
> 	rectRotate->rotate(30);
> 	return app.exec();
> }
>
>

--
 [ signature omitted ]