PDA

View Full Version : QGraphicsLineItem moveBy() doesn't really move the line, why?



jiapei100
7th March 2013, 23:26
Hi, all:

My code is as simple as:


QPointF end1 = dynamic_cast<QGraphicsLineItem*>(lineItem)->line().p1();
QPointF end2 = dynamic_cast<QGraphicsLineItem*>(lineItem)->line().p2();
std::cout << end1.x() << " " << end1.y() << std::endl;
std::cout << end2.x() << " " << end2.y() << std::endl;

dynamic_cast<QGraphicsLineItem*>(lineItem)->moveBy(dx, dy); // I'm pretty sure dx and dy are not 0

end1 = dynamic_cast<QGraphicsLineItem*>(lineItem)->line().p1();
end2 = dynamic_cast<QGraphicsLineItem*>(lineItem)->line().p2();
std::cout << end1.x() << " " << end1.y() << std::endl;
std::cout << end2.x() << " " << end2.y() << std::endl;

two outputs of end1/end2 are always the same. However, I'm expecting there should be some difference, right?
Why moveBy() doesn't really move two end points of such a QGraphicsLineItem ?
How can I successfully move the end points then?


cheers
Pei

wysota
8th March 2013, 00:26
two outputs of end1/end2 are always the same. However, I'm expecting there should be some difference, right?
No, they should be the same. You're not changing the line, just moving it.


Why moveBy() doesn't really move two end points of such a QGraphicsLineItem ?
Because the line determines the bounding rect of the item, not its position.


How can I successfully move the end points then?
You can learn about the difference between graphics item coordinate space and position.

If you have a long stick in your hand and you say that it starts "two metres from here" and ends "three metres from here" and you move two steps in whatever direction (still holding the stick), it still starts "two metres from here" and ends "three metres from here". It's just "here" that changed because you moved. The same if you call moveBy() -- you will see pos() change but not boundingRect() or line().

jiapei100
10th March 2013, 21:34
Thank you wysota.
Very clear explanation. Thanks ...

Trying to play with QPainterPath these days, a bit tough.
Any examples please?



Cheers
Pei


No, they should be the same. You're not changing the line, just moving it.


Because the line determines the bounding rect of the item, not its position.


You can learn about the difference between graphics item coordinate space and position.

If you have a long stick in your hand and you say that it starts "two metres from here" and ends "three metres from here" and you move two steps in whatever direction (still holding the stick), it still starts "two metres from here" and ends "three metres from here". It's just "here" that changed because you moved. The same if you call moveBy() -- you will see pos() change but not boundingRect() or line().

wysota
10th March 2013, 22:20
Examples of what exactly?

jiapei100
11th March 2013, 00:39
Hi, wysota:

^_^
Please refer to my topic at
http://www.qtcentre.org/threads/53616-Any-Qt-Shape-Widgets-packages?highlight=
or
http://qt-project.org/forums/viewthread/25503/

I want to drag and drop any 2D (later I'll do 3D) shapes, and hopefully, I can rotate, scale, translate this shape due to various mouse actions.

I did some example, but for now, my program needs to select the shape first, then drag one end of the line to any place I specified. I don't think it's very convenient.
I'm wondering if there are some examples for me to follow up.

Thanks wysota !


Best Regards
Pei




Examples of what exactly?