Weird behaviour when dragging QGraphicsItem
-
Here's before I drag:
Here's after I drag it:
If I alt tab the blurry shadow line disappears, what I can't figure out is why it appears in the first place.
Here's the code I'm using for my objects:
//rectItem customRect::customRect(const QRectF &rect) : QGraphicsRectItem(rect){ setFlag(QGraphicsItem::ItemIsMovable); setFlag(QGraphicsItem::ItemSendsScenePositionChanges); } void customRect::addLine(myArrow *line) { arrows.append(line); this->line = line; } QVariant customRect::itemChange(GraphicsItemChange change, const QVariant &value) { if (change == ItemPositionChange && scene()) { QPointF newPos = value.toPointF(); moveLineToCenter(newPos); } return QGraphicsItem::itemChange(change, value); } void customRect::moveLineToCenter(QPointF newPos) { int xOffset = rect().x(); int yOffset = rect().y(); QPointF newCenterPos = QPointF(newPos.x() + xOffset, newPos.y() + yOffset); // Move the required point of the line to the center of the elipse QPointF p1 = newCenterPos; p1.setY( newCenterPos.y() - 200 ); QPointF p2 = newCenterPos; foreach (myArrow *arrow, arrows) { p1.setX(p1.x() + 25); p2.setX(p2.x() + 25); arrow->updatePosition(p1,p2); update(); } } //arrowItem myArrow::myArrow(customRect *rect){ myrect = rect; QPointF p = rect->boundingRect().topLeft(); p.setY(p.y() - 200); setLine( QLineF( p , rect->boundingRect().topLeft() )); setFlag(QGraphicsLineItem::ItemIsSelectable); } void myArrow::updatePosition(QPointF p1, QPointF p2) { QLineF line(p2,p1); setLine(line); } void myArrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { qreal arrowSize = 20; double angle = std::atan2(-line().dy(), line().dx()); QPointF arrowP1 = line().p1() + QPointF(sin(angle + M_PI / 3) * arrowSize, cos(angle + M_PI / 3) * arrowSize); QPointF arrowP2 = line().p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize, cos(angle + M_PI - M_PI / 3) * arrowSize); arrowHead.clear(); arrowHead << line().p1() << arrowP1 << arrowP2; painter->drawLine(line()); painter->drawPolygon(arrowHead); }
I really have no clue, please help.
Thanks in advance!(Editted myArrow paint event)
-
Hi,
Isn't that your arrow being painted as you are moving your item ?
-
Why are you calling update from within paint ?
Then are you sure that you are painting the same thing each time ?
Take a look at the notes in the paint method documentation.
-
Why are you calling update from within paint ?
Then are you sure that you are painting the same thing each time ?
Take a look at the notes in the paint method documentation.
@SGaist I'm trying to replicate this example with only 1 object (which I thought would be easier, since I'm still learning Qt) : http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-arrow-cpp.html
The update inside paint event was misplaced but it was not causing the problem. If I take out this line:
painter->drawPolygon(arrowHead)
It works fine but I lose the head of my arrow. What really annoys me is that when I alt tab and tab back again to my program the blurry lines disappears, which makes me think its not a problem within my code (?).