PDA

View Full Version : QGraphicsLineItem - setting its points



Durkin
5th March 2014, 00:52
Hello,

I 'm looking to have a QGraphicsLineItem and be able to move either of its points. Between its position and its QLine, I 've failed to determine an effective way of accomplishing this. In particular, I am baffled as to why this prints both points at 0,0:



#include <QGraphicsLineItem>
#include <QDebug>

int main()
{
QGraphicsLineItem* item = new QGraphicsLineItem;
item->line().setP1(QPointF(10, 10));
qDebug() << item->line();
}


Any clues would be welcome.

ChrisW67
5th March 2014, 01:10
Line 7: item->line() returns a temporary copy of the item's internal QLineF object. You modify that temporary object and then it is discarded.
Line 8: Prints the content of another (unchanged) copy of the line.



// To set both line ends
QGraphicsLineItem* item = new QGraphicsLineItem;
item->setLine(QLineF(QPointF(10, 10), QPointF(20, 20)));
qDebug() << item->line();

// To move one end of the line
QLineF line = item->line();
line.setP1(QPointF(30, 30));
item->setLine(line);
qDebug() << item->line();