Unsolved Problem with QGraphicsLineItem, it keeps "flashing"
-
Hi,
In my program i can connect lines between objects(QGraphicItem) in a diagramscene
First i drew the lines between the objects position(object->pos()), but i wanted to connect the lines in the center of the object. So the program draws a line using the position (object->pos()+p), P is a QPointF.But now i have a problem, when i move the objects and the line keeps updating, it flashes. Like it follows the position
object->pos()+p but sometimes for some miliseconds it comeback to the object->pos()I don't know if i'm being clear...
https://www.youtube.com/watch?v=MLcGfTGzFOw
here is a video that i recorded with my cellphoneDiagramscene.cpp
void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) { ... case InsertLine: line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(), mouseEvent->scenePos())); line->setPen(QPen(myLineColor, 2)); addItem(line); break; ... } void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) { if (myMode == InsertLine && line != 0) { QLineF newLine(line->line().p1(), mouseEvent->scenePos()); line->setLine(newLine); } else if (myMode == MoveItem) { QGraphicsScene::mouseMoveEvent(mouseEvent); } }
Arrow.cpp(that's the class that handles the line function)
void Arrow::updatePosition() { QLineF line(mapFromItem(myStartItem,p1), mapFromItem(myEndItem,p2));//p1 and p2 here setLine(line); update(); } void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { if (myStartItem->collidesWithItem(myEndItem)) return; QPen myPen = pen(); myPen.setColor(myColor); painter->setPen(myPen); painter->setBrush(myColor); QPointF start(myStartItem->pos()+p1); QPointF end(myEndItem->pos()+p2);//trid to add the points here, but nothing happens QLineF centerLine(myStartItem->pos(), myEndItem->pos()); painter->drawLine(line()); setLine(QLineF(start+p1,end+p2)); if (isSelected()) { painter->setPen(QPen(myColor, 1, Qt::DashLine)); QLineF myLine = line(); myLine.translate(0, 4.0); painter->drawLine(myLine); myLine.translate(0,-8.0); painter->drawLine(myLine); } }
i don't have any ideia how to fix it, can somebody help me please?
-
Hi,
Are you sure you are not replacing the line on each move ?
-
how can i be certain?that's everything related to moving
-
You can add a qDebug statement so you know where it's going.
-
I don't know why but if i use just
setLine(QLineF(myStartItem->pos(),myEndItem->pos()));
instead of
setLine(QLineF(myStartItem->pos()+p,myEndItem->pos()+p)); //p is abitrary point
it works just fine, doesn't flashes
and it makes no sense to me