Results 1 to 2 of 2

Thread: QGraphicsLineItem's setAngle method doesn't work

  1. #1
    Join Date
    May 2014
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsLineItem's setAngle method doesn't work

    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:

    Qt Code:
    1. //some code....
    2.  
    3. secondPoint = new QGraphicsItemGroup(0);
    4. QLineF perpendicLine(QLineF(firstPoint->scenePos(), point).normalVector().unitVector());
    5. perpendicLine.setLength(200);
    6. QPointF centerPoint = perpendicLine.pointAt(0.5);
    7. perpendicLine.translate(-centerPoint.x(), -centerPoint.y());
    8.  
    9. perpLine = new QGraphicsLineItem(perpendicLine);
    10. perpLine->setPen(QPen(Qt::red));
    11. secondPointGroup->addToGroup(perpLine);
    12.  
    13. this->addItem(secondPoint);
    14.  
    15. //some code....
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void TwoDotsGraphicsScene::correctLineAngle(int angle)
    2. {
    3. if(secondPoint == NULL)
    4. return;
    5.  
    6. qDebug() << "agnle " << angle;
    7. perpLine->line().setAngle(angle);
    8. qDebug() << "lineAngle " << perpLine->line().angle();
    9.  
    10. calculateDistance();
    11. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsLineItem's setAngle method doesn't work

    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.
    Last edited by d_stranz; 20th May 2014 at 03:46.

Similar Threads

  1. Replies: 4
    Last Post: 10th March 2013, 23:39
  2. QDomDocumen save method doesn't work!
    By xiongxiongchuan in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2010, 11:44
  3. QThread::start() doesn't invoke run() method
    By seim in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2009, 17:09
  4. QHttp RFC Method PUT not work.
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 19th May 2007, 15:44
  5. Replies: 6
    Last Post: 3rd November 2006, 11:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.