_p2
= _p2
+ QPointF(0,
20);
// the calculation is just random, to place arrow base at above or below parent line.const QPointF center
= (_p1
+_p2
)/2;
_p2 = _p2 + QPointF(0,20); // the calculation is just random, to place arrow base at above or below parent line.
const QPointF center = (_p1+_p2)/2;
To copy to clipboard, switch view to plain text mode
You have changed _p2 of "this" object in a place where you shouldn't, this method should only place the child line in correct position. In this method just use the existing _p1 and _p2 of the parent item to calculate new _p1 and _p2 of child, nothing more - look at the method name, "updateArrowItem", and not "updateMyPositionWithRandomData()".
If you keep changing _p2 like that in this method I doubt you can predict the final result.
Don't do "random" calculations, according to your specs it shouldn't be random. If you want to translate the base point for attachement, do that correctly using parent line normal vector, for example:
void Line::updateArrowItem(){
if (_arrow) {
// this will attach arrow sligthly below parent line, near the second point
normal.setLength(10);
const QPointF center
= p
- (normal.
p2()-normal.
p1());
// move the base point below the parent line normal.setLength(30); // or whatever else arrow length you need
const QPointF head
= center
+ (normal.
p2()-normal.
p1());
// change '+' to '-' in order to point the arrow in other direction _arrow->_p1 = this->mapToItem(_arrow, center); // assume _p1 = arrow base, _p2 = arrow head
_arrow->_p2 = this->mapToItem(_arrow, head);
}
}
void Line::updateArrowItem(){
if (_arrow) {
QLineF normal = QLineF(_p1,_p2).normalVector();
// this will attach arrow sligthly below parent line, near the second point
normal.setLength(10);
const QPointF p = QLineF(_p1,_p2).pointAt(0.8);
const QPointF center = p - (normal.p2()-normal.p1()); // move the base point below the parent line
normal.setLength(30); // or whatever else arrow length you need
const QPointF head = center + (normal.p2()-normal.p1()); // change '+' to '-' in order to point the arrow in other direction
_arrow->_p1 = this->mapToItem(_arrow, center); // assume _p1 = arrow base, _p2 = arrow head
_arrow->_p2 = this->mapToItem(_arrow, head);
}
}
To copy to clipboard, switch view to plain text mode
I have tried it and it works ok, you are very close to the solution.
Bookmarks