[Interest] Draw multiple rectangles in a QGraphicsItem using the mouse

david.carmona93 at gmail.com david.carmona93 at gmail.com
Sat Dec 26 13:00:05 CET 2015


Hi everybody,



I am currently trying to resize a QGraphicsRectItem through my QGraphicsScene using the MouseMoveEvent method.

In the latter, I’m checking if the application is in the “startModifying” mode (Therefore, if he wants to modify a rectangle) and in which corner the moue is:

IsOnTopLeftCornerPressed: The user is pressing  the rectangle’s top left corner


IsOnTopRightCornerPressed: The user is pressing the rectangle’s top right corner


IsOnBottomLeftCornerPressed: The user is pressing the rectangle’s bottom left corner


IsOnBottomRightCornerPressed: The user is pressing the rectangle’s bottom right corner




However, it does not work at all. Could you help me please ?



Best regards



Here you have the code:



void ImageGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
    if(modifyButton==true&&isSelected==true){
       this->checkMouseMove(event->scenePos());
    }


    if(startModifying==true){


      if(isOnTopLeftCornerPressed==true){
         


         // The normalized method allow the user to  draw rectangles having a negative height/width
         // Remember: The top-left point is in the rectangle's local coordinates !!!
         QRectF rectangleF(0,0,event->scenePos().x()-rectModified->rect().bottomRight().x(),event->scenePos().y()-rectModified->rect().bottomRight().y());
         rectModified->setRect(rectangleF.normalized());
         
         rectModified->setRect(rectangleF);
         this->modifyButton=false;
      }
      if(isOnTopRightCornerPressed==true){
          qDebug()<<"I am here in topRight";
          rectModified->setPos(rectModified->getBottomLeft().x(),rectModified->getBottomLeft().y());


          // The normalized method allow the user to  draw rectangles having a negative height/width
          // Remember: The top-left point is in the rectangle's local coordinates !!!
          QRectF rectangleF(0,0,event->scenePos().x()-rectModified->getBottomLeft().x(),event->scenePos().y()-rectModified->getBottomLeft().y());
          rectModified->setRect(rectangleF.normalized());
      }
      if(isOnBottomLeftCornerPressed==true){
          
          // The normalized method allow the user to  draw rectangles having a negative height/width
          // Remember: The top-left point is in the rectangle's local coordinates !!!
          QRectF rectangleF(0,0,event->scenePos().x()-rectModified->getTopRight().x(),event->scenePos().y()-rectModified->getTopRight().y());
          rectModified->setRect(rectangleF.normalized());
      }


      if(isOnBottomRightCornerPressed==true){


          // The normalized method allow the user to  draw rectangles having a negative height/width
          // Remember: The top-left point is in the rectangle's local coordinates !!!
          QRectF rectangleF(0,0,event->scenePos().x()-rectModified->getPoint().x(),event->scenePos().y()-rectModified->getPoint().y());
          rectModified->setRect(rectangleF.normalized());
      }


    }else{
    }


    /*
     * Draw the Rectangle
     */
    if(mouseLeftPressed==true&&drawButton==true){


      if(!rect){
          rect=new RectangleDraw;
          this->addItem(rect);
          rect->setPen(QPen(Qt::red,4,Qt::SolidLine));
          rect->setPos(origPoint.x(),origPoint.y());
          rect->setId(id=id+1);
     }


      // The normalized method allow the user to  draw rectangles having a negative height/width
      // Remember: The top-left point is in the rectangle's local coordinates !!!
      QRectF rectangleF(0,0,event->scenePos().x()-origPoint.x(),event->scenePos().y()-origPoint.y());


      rect->setRect(rectangleF.normalized());


      // The top-Left point is in local rectangle's coordinates. Therefore, we have to transform them in scene's absolute
      // coordinates using mapToScene.
      QPointF point;
      point=rect->mapToScene(rect->rect().topLeft());
      rect->setPoint(point);


    } else{
      }


        QGraphicsScene::mouseMoveEvent(event);
}











De : Michael Sué
Envoyé : ‎lundi‎ ‎26‎ ‎octobre‎ ‎2015 ‎12‎:‎58
À : david.carmona93 at gmail.com, interest at qt-project.org





Hi David,

> However, I have some problems when I actually want to draw rectangles with negatives width and height. I mean when the values of origPoint.x() and 
> origPoint.y() are strictly higher than scenePos().x() and scenePos().y() respectively.

You will have to normalize the rectangle coordinates. There is a QRect method for this. It exchanges coordinates so that width and height is positive. You can set the rectangle then.

- Michael.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20151226/add4e5ee/attachment.html>


More information about the Interest mailing list