Problem while removing QGraphicsPathItem from Scene after move.
-
Hi everyone....
I am doing free hand drawing on QGraphicsView and QGraphicsScene. Following is my code:-
@WhiteBoardView::WhiteBoardView()
{
mDrawnPathItem = mWhiteBoardScene->addPath(QPainterPath());
mDrawnPathItem->setBrush( QColor(...) );
mDrawnPathItem->setPen(Qt::NoPen);
}void WhiteBoardView::drawLineTo(const QPointF &previous, const QPointF ¤t, const QPen& drawPen)
{
QGraphicsLineItem lineHelper;
lineHelper.setLine( QLineF(previous, current) );
lineHelper.setPen(drawPen);QPainterPath path = mDrawnPathItem->path(); path.setFillRule(Qt::WindingFill); path.addPath( lineHelper.shape() ); path = path.simplified(); mDrawnPathItem->setPath(path);
m_DrawnItems << mDrawnPathItem;@
//mDrawnPathItem is of type QGraphicsPathItem* and was added in the constructor with an empty QPainterPath() and m_DrawnItems is a list of QGraphicsPathItem* .
Following code is to erase drawn line from scene :-
@void WhiteBoardView::EraserLineTo(const QPointF ¤t, double radius)
{
QPainterPath eraserPath = mEraserPathItem->path();
eraserPath.addEllipse(current, 20,20);//m_DrawnItems = QList<QGraphicsPathItem*> foreach( QGraphicsPathItem* item, m_DrawnItems) { QPainterPath drawnPath = item->path(); drawnPath.subtracted(eraserPath) item->setPath(drawnPath); } }@
I am doing drawing easily and removing drawn lines easily. But, If i move drawn line on scene and then remove or erase that moved line, it does not erase. I think, i am not getting the updated points of path.
Thanks in advance.... :)
-
how do you move the items exactly?
just a guess from me:
@
void WhiteBoardView::EraserLineTo(const QPointF ¤t, double radius)
{
QPainterPath eraserPath = mEraserPathItem->path();
eraserPath.addEllipse(current, 20,20);//m_DrawnItems = QList<QGraphicsPathItem*> foreach( QGraphicsPathItem* item, m_DrawnItems) { QPainterPath drawnPath = item->path(); drawnPath.subtracted(eraserPath.translated(item->pos())) item->setPath(drawnPath); } }
@
-
Thanks to reply Raven.. Sorry but i am not moving like this. Following is my code to move:-
@
void WhiteBoardView::mousePressEvent (QMouseEvent *event)
{
previousPt = event->pos(); // previousPt is QPointvbMousePressed = true; // vbMousePressed is bool type variable if(event->button() == Qt::LeftButton) { itemUnderMouse = this->itemAt(event->pos()); // itemUnderMouse is QGraphicsItem* }
}
void WhiteBoardView::mouseMoveEvent (QMouseEvent *event)
{
vbMouseMoving = true;if(event) { if(vbMousePressed && vbMouseMoving) { if(itemUnderMouse == NULL) { return; } else { moveSelectedImg(event->pos()); }
}
}
}// Following function is used to move polygon.
void WhiteBoardView::moveSelectedImg(const QPoint &currnt)
{
if(currnt == previousPt || itemUnderMouse == NULL)
return;int nXCor = currnt.x() - previousPt.x(); int nYCor = currnt.y() - previousPt.y(); itemUnderMouse->moveBy(nXCor, nYCor); previousPt = currnt;
}
@
-
well...are you sure!?
check the "docs":http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#moveBy and try my code pls. -
I could not move the QGraphicsPathItem according to your following code.
@void WhiteBoardView::EraserLineTo(const QPointF ¤t, double radius)
{
QPainterPath eraserPath = mEraserPathItem->path();
eraserPath.addEllipse(current, 20,20);//m_DrawnItems = QList<QGraphicsPathItem*> foreach( QGraphicsPathItem* item, m_DrawnItems) { QPainterPath drawnPath = item->path(); drawnPath.subtracted(eraserPath.translated(item->pos())) item->setPath(drawnPath); } }@
-
Actually, i am moving the drawn QGraphicsPathItem over the QGraphicsView. This works fine. After moving, when i use eraser and move eraser over the new position of QGraphicsPathItem, it does not remove it but if i move eraser over its(QGraphicsPathItem's) last position, then it removes.
-
I have checked that i am not able to find updated path of QGraphicsPathItem after using moveBy() function and also not able to store updated path in m_DrawnItems list.
-
try this:
@
drawnPath.subtracted(eraserPath.translated(-item->pos()))
@or maybe it's easier to you if you dont move the item itself but only it's containing painterpath? Then there should also no changes be necessary in your eraser code.
@
void WhiteBoardView::moveSelectedImg(const QPoint &currnt)
{
if(currnt == previousPt || itemUnderMouse == NULL)
return;int nXCor = currnt.x() - previousPt.x(); int nYCor = currnt.y() - previousPt.y(); QPainterPath path = ....; // get painter path from itemUnderMouse path.translate(nXCor, nXCor); ... //set path back to item previousPt = currnt;
}
@ -
Ok.... But tell me that, should i use the following function with your suggestion ???
@/ Following function is used to move polygon.
void WhiteBoardView::moveSelectedImg(const QPoint &currnt)
{
if(currnt == previousPt || itemUnderMouse == NULL)
return;int nXCor = currnt.x() - previousPt.x(); int nYCor = currnt.y() - previousPt.y(); itemUnderMouse->moveBy(nXCor, nYCor); previousPt = currnt;
}@
-
Sry, but it also does not work .... :(
-
Raven, actually i forgot to tell you the code which execute on mouseRelease(). Sorry for this. Following code will execute on mouseRelease:-
@allItemsList = this->scene()->items();
qDebug() << "size of AllItemList" << allItemsList.size(); if(this->scene() != NULL) { this->setScene(mController->newSceneForView()); // It sets the new scene after translation of QGraphicsPathItems. this->setSceneRect(qApp->desktop()->rect()); setGraphicsPathItem(this->scene()); // It sets QGraphicsPathItems for new Scene. mController->updateBackground(); // It sets the white background. for(int i=0; i<allItemsList.size(); i++) { QGraphicsItem* item = allItemsList[i]; if(mSelectedPolygonItem == qgraphicsitem_cast<QGraphicsPathItem*>(item)) { this->scene()->addItem(item); if(m_DrawnItems.contains(mSelectedPolygonItem)) { for(int i=0; i<m_DrawnItems.size(); i++) { if(m_DrawnItems[i] == mSelectedPolygonItem) { m_DrawnItems[i]->setPath(returnedPath); } } } } else { this->scene()->addItem(item); } } } allItemsList.clear();@