PDA

View Full Version : problem in QLinef()



wagmare
26th February 2009, 14:17
hi friends ,
i need a help to clarify my doubt in QLineF() .. i dont know where i am commiting the mistake ..

in mainwindow.cpp (QGraphicsView())
i create a line item class ....


line1 = new LineItem(QLineF(2,4, 132 ,160), Qt::blue);
line1->setPos(374,215);

and
in lineitem.cpp


LineItem::LineItem(const QLineF &line, const Qt::GlobalColor color)
: QGraphicsLineItem(line),
color(color)

void LineItem ::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setPen(QColor(color));
QReal value = 90;
line().setAngle(value);
painter->drawLine(line());
}


here the line is drawn perfectly but i cant fix the angle ... its not turning in counter clockwise ... not like transform.rotate .. why ?

please help ...

wysota
26th February 2009, 22:26
You shouldn't change the angle like that. The paint() routine should always paint the item the same way, regardless of its actual position or rotation on the scene. If you want to rotate the line, use rotate() on the item, but still draw the item as if it was in its original direction (i.e. horizontal).

wagmare
27th February 2009, 05:28
You shouldn't change the angle like that. The paint() routine should always paint the item the same way, regardless of its actual position or rotation on the scene. If you want to rotate the line, use rotate() on the item, but still draw the item as if it was in its original direction (i.e. horizontal).

thanks for reply ..
transform.rotate(90) is working fine ... but how can i use this QLineF::setAngle() function ... where can i implement it ..

wagmare
27th February 2009, 06:47
hi friends,
i try using QTransform as

in mainwindow.cpp (GraphicsView())


line1 = new LineItem(QLineF(2,4, 132 ,160), Qt::blue, 90); //creating an lineItem in scene
line1->setPos(374,215); //settting the line position in screen
scene->addItem(line1);


in lineitem.cpp


LineItem::LineItem(const QLineF &line, const Qt::GlobalColor color, qreal angle)
: QGraphicsLineItem(line),
color(color) //constuctor
{
qreal value = angle;
}




void LineItem ::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
QTransform transform;
painter->setPen(QColor(color));
if(value){
transform.translate(300,300); // here comes the problem
transform.rotate(value);
painter->setTransform(transform);
painter->drawLine(line());
}else{
line().setAngle(value);
painter->drawLine(line());
}
}


in this program if the user send the the angle of rotation the line with the same coordinates i try to rotate ... but without this
transform.translate(x,y); i cant see the line ... more the


line1->setPos(374,215); // graphicasScene

this value also not working .. why ? it is defaulter to top left of the screen ...

but if there is no angle argument like


line1 = new LineItem(QLineF(2,4, 132 ,160), Qt::blue ); //without angle argument

its working fine ... i can setPos() the item ... why? ..

please help

wysota
27th February 2009, 09:39
but how can i use this QLineF::setAngle() function ... where can i implement it ..

Don't use it with QGraphicsLineItem, it doesn't make much sense to do so in this context. Rotate the item, not the line within it.

wagmare
27th February 2009, 09:44
thanks .. i will rotate the item as


QTransform transform;
transform.rotate(90);
line1 = new LineItem(QLineF(1,1, 132 ,160), Qt::blue, 90);
scene->addItem(line1);
line1->setPos(574,415);
line1->setTransform(transform);