PDA

View Full Version : QGraphicsLineItem's setAngle method doesn't work



fantomasdnb
19th May 2014, 22:16
Hello, everyone.

So, I have a class called TwoDotsGraphicsScene which subclasses QGraphicsScene. In some place I add a QGraphicsGroup to it, where one of the items is QGraphicsLineItem:



//some code....

secondPoint = new QGraphicsItemGroup(0);
QLineF perpendicLine(QLineF(firstPoint->scenePos(), point).normalVector().unitVector());
perpendicLine.setLength(200);
QPointF centerPoint = perpendicLine.pointAt(0.5);
perpendicLine.translate(-centerPoint.x(), -centerPoint.y());

perpLine = new QGraphicsLineItem(perpendicLine);
perpLine->setPen(QPen(Qt::red));
secondPointGroup->addToGroup(perpLine);

this->addItem(secondPoint);

//some code....


perpLine - is a class public variable.

Next. This class has a slot correctLineAngle(int angle), in which I'd like to change a line's angle:



void TwoDotsGraphicsScene::correctLineAngle(int angle)
{
if(secondPoint == NULL)
return;

qDebug() << "agnle " << angle;
perpLine->line().setAngle(angle);
qDebug() << "lineAngle " << perpLine->line().angle();

calculateDistance();
}


But debug output is kind of like:

lineAngle 93.7022
agnle 121
lineAngle 93.7022
agnle 127
lineAngle 93.7022
agnle 133
lineAngle 93.7022
agnle 136
lineAngle 93.7022

So, I guess setAngle not having an effect. What should I do?

d_stranz
20th May 2014, 04:42
perpLine->line().setAngle(angle);

So it looks like perp->line() is returning a temporary QLineF, which of course lets you set its angle, and then it promptly goes out of scope at the end of the statement and is destroyed. Likewise, the next statement (the qDebug()) is returning yet another temporary QLineF, whose angle of course has not been changed, since it's probably a copy of some QLineF internal to your class that was never touched by the previous statement.

If you really want to change the angle of the line internal to your class, then perpLine->line() should return a QLineF * to the internal instance, not a QLineF copy of it.