Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem while removing QGraphicsPathItem from Scene after move.
Qt 6.11 is out! See what's new in the release blog

Problem while removing QGraphicsPathItem from Scene after move.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 5.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Himanshu Rohilla
    wrote on last edited by
    #1

    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 &current, 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 &current, 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.... :)

    HImanshu Rohilla

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      how do you move the items exactly?

      just a guess from me:
      @
      void WhiteBoardView::EraserLineTo(const QPointF &current, 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);
              }
          }
      

      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Himanshu Rohilla
        wrote on last edited by
        #3

        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 QPoint

        vbMousePressed = 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;
        

        }

        @

        HImanshu Rohilla

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          well...are you sure!?
          check the "docs":http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#moveBy and try my code pls.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Himanshu Rohilla
            wrote on last edited by
            #5

            I could not move the QGraphicsPathItem according to your following code.

            @void WhiteBoardView::EraserLineTo(const QPointF &current, 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);
                    }
                }@
            

            HImanshu Rohilla

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Himanshu Rohilla
              wrote on last edited by
              #6

              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.

              HImanshu Rohilla

              1 Reply Last reply
              0
              • H Offline
                H Offline
                Himanshu Rohilla
                wrote on last edited by
                #7

                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.

                HImanshu Rohilla

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  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;
                  

                  }
                  @

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    Himanshu Rohilla
                    wrote on last edited by
                    #9

                    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;
                    

                    }@

                    HImanshu Rohilla

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      Himanshu Rohilla
                      wrote on last edited by
                      #10

                      Sry, but it also does not work .... :(

                      HImanshu Rohilla

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        Himanshu Rohilla
                        wrote on last edited by
                        #11

                        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();@
                        

                        HImanshu Rohilla

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          smudibh20
                          wrote on last edited by
                          #12

                          Hey I want to do similar operation of moving a QGraphicsPathItem.
                          But I want to detect the movement, get the current coordinates of mouse pointer and redraw the path contained in that Item according to current coordinates.
                          How to do that?

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved