PDA

View Full Version : Rotating QGraphicsItem



Gopala Krishna
19th December 2006, 13:28
Hello,
I want to rotate graphicsitems when I right click it. It should rotate progressively i.e for one click the angle with horizontal should be 90 deg, for 2nd click it should be 180(with horizontal) and so on.
But it doesn't clear the area (occupied by it before rotation ) when the angle is 270 or 90 and then it is right clicked. It looks as though two items are present. I tried by adding calls to update manually, but in vain.
What am I doing wrong ? :confused:
Please help me solve this.


#include <QtGui>

class Rotor : public QGraphicsItem
{
public:
Rotor(QGraphicsScene *s) : QGraphicsItem(0l,s)
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
}

void mousePressEvent(QGraphicsSceneMouseEvent * e)
{

QRectF br = QRectF(mapToScene(boundingRect().topLeft()),boundi ngRect().size());

if(e->buttons() & Qt::RightButton)
rotate(90);
scene()->update(br);
QGraphicsItem::mousePressEvent(e);
}

void paint(QPainter *p,const QStyleOptionGraphicsItem * , QWidget *)
{
p->drawLine(-18, -9, 18, -9);
p->drawLine( 18, -9, 18, 9);
p->drawLine( 18, 9,-18, 9);
p->drawLine(-18, 9,-18, -9);
p->drawLine(-30, 0,-18, 0);
p->drawLine( 18, 0, 30, 0);
}

QRectF boundingRect() const
{
return QRectF(-30,-9,60,18);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QGraphicsScene scene(0,0,250,250);
QGraphicsView* view = new QGraphicsView(&scene);
view->setRenderHint(QPainter::Antialiasing);

Rotor *r = new Rotor(&scene);
r->setPos(125,125);
r = new Rotor(&scene);
r->setPos(57,75);
view->show();

return app.exec();
}

wysota
20th December 2006, 11:30
It works correctly for me on Linux with Qt4.2.2

Bitto
20th December 2006, 21:58
The workaround for < 4.2.2 is to call update also before the rotate.

Gopala Krishna
21st December 2006, 12:50
Thanks for the tip ! :)