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 drawing in QGraphicsView
Qt 6.11 is out! See what's new in the release blog

Problem drawing in QGraphicsView

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by ivanicy
    #1

    Hello!

    I am drawing a rectangle in a QGraphicsView, but I have a problem doing it:

    1cbcb9ca-d756-437e-b16a-61a80fbeb7c9-InkedWhatsApp Image 2021-11-08 at 9.34.33 AM_LI.jpg

    This happens when I move the mouse to draw the rectangle.

    How can I fix this? When I release the mouse and finish the rectangle, this lines disapear, but it isn't very visual, you know.

    Thank you very much!

    JonBJ 1 Reply Last reply
    0
    • ivanicyI ivanicy

      Hello!

      I am drawing a rectangle in a QGraphicsView, but I have a problem doing it:

      1cbcb9ca-d756-437e-b16a-61a80fbeb7c9-InkedWhatsApp Image 2021-11-08 at 9.34.33 AM_LI.jpg

      This happens when I move the mouse to draw the rectangle.

      How can I fix this? When I release the mouse and finish the rectangle, this lines disapear, but it isn't very visual, you know.

      Thank you very much!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @ivanicy
      How are you doing this? Are you using a QGraphicsRectItem?

      1 Reply Last reply
      0
      • ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

        I am using my own class, setting the nodes and the edges. But I was asking about some property in QGraphicsView that solves this problem, but I understand that is a bit more difficult.

        JonBJ artwawA 2 Replies Last reply
        0
        • ivanicyI ivanicy

          I am using my own class, setting the nodes and the edges. But I was asking about some property in QGraphicsView that solves this problem, but I understand that is a bit more difficult.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @ivanicy
          Then I cannot answer about your own class, looks like you are leaving some trail of lines behind which later get wiped away on refresh. QGraphicsView works by being told what areas of the canvas to update. If you draw something non-permanent it will stay there until you cause that area of the view to be updated, which may leave "artefacts" if you don't get it right.

          1 Reply Last reply
          0
          • ivanicyI ivanicy

            I am using my own class, setting the nodes and the edges. But I was asking about some property in QGraphicsView that solves this problem, but I understand that is a bit more difficult.

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #5

            @ivanicy can you show that class please?

            For more information please re-read.

            Kind Regards,
            Artur

            ivanicyI 1 Reply Last reply
            0
            • artwawA artwaw

              @ivanicy can you show that class please?

              ivanicyI Offline
              ivanicyI Offline
              ivanicy
              wrote on last edited by ivanicy
              #6

              It's a very long class, but here are the important functions:

              Variables:

                  QGraphicsScene *m_scene;
                  int id;
                  CNode* nodeFirst;
                  CNode* nodeSecond;
                  QGraphicsRectItem *graphRect;
                  QGraphicsSimpleTextItem *textRoi;
                  bool m_bExists;
                  bool m_bFirstNodeExist;
                  bool m_bSelected;
                  CNode* nodeMax;
                  CNode* nodeMin;
                  int m_iId;
                  int m_iType;
                  int m_iTarget;
                  QString m_sId;
                  ROIType m_RoiType;
                  QPointF m_topLeft, m_topRight, m_bottomLeft, m_bottomRight;
                  int globalIncX, globalIncY;
                  QPoint punto;
                  QString m_sPathNames;
                  double m_dCurrentScale;
              

              PRESS:

              void CRoi::press(QPointF event, QPointF limitTopLeft, QPointF limitBottomRight){
                  if(!m_bFirstNodeExist){
                      nodeFirst->setPos(event.x(), event.y());
                      if(nodeFirst->pos().x() < limitTopLeft.x()){
              //            nodeFirst->setPos(limitTopLeft.x(), nodeFirst->pos().y());
                          nodeFirst->setX(limitTopLeft.x());
                      }
                      if(nodeFirst->pos().x() > limitBottomRight.x()){
              //            nodeFirst->setPos(limitBottomRight.x(), nodeFirst->pos().y());
                          nodeFirst->setX(limitBottomRight.x());
                      }
              
                      if(nodeFirst->pos().y() < limitTopLeft.y()){
              //            nodeFirst->setPos(nodeFirst->pos().x(), limitTopLeft.y());
                          nodeFirst->setY(limitTopLeft.y());
                      }
                      if(nodeFirst->pos().y() > limitBottomRight.y()){
              //            nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                          nodeFirst->setY(limitBottomRight.y());
                      }
              
                      switch (m_iType) {
                      case 0:
              //            graphRect->setPen(QPen(QColor(0x0000FF), 1, Qt::SolidLine));
              //            textRoi->setBrush(QBrush(QColor(0x0000FF)));
                          graphRect->setPen(QPen(QColor(0xFF871B), 1*m_dCurrentScale, Qt::SolidLine));
                          textRoi->setBrush(QBrush(QColor(0xFF871B)));
                          m_sId = getSType();//QString("RP%1").arg(QString::number(m_iId+1));
                          textRoi->setText(QString("%1 %2").arg(getSType(), QString::number(m_iId+1)));
                          break;
                      case 1:
                          graphRect->setPen(QPen(QColor(0xFF0000), 1*m_dCurrentScale, Qt::SolidLine));
                          textRoi->setBrush(QBrush(QColor(0xFF0000)));
                          m_sId = getSType();//QString("RP%1").arg(QString::number(m_iId+1));
                          textRoi->setText(QString("%1 %2").arg(getSType(), QString::number(m_iId+1)));
                          break;
                      case 2:
                          graphRect->setPen(QPen(QColor(0xFFFB00), 1*m_dCurrentScale, Qt::SolidLine));
                          textRoi->setBrush(QBrush(QColor(0xFFFB00)));
                          m_sId = getSType();//QString("RP%1").arg(QString::number(m_iId+1));
                          textRoi->setText(QString("%1 %2").arg(getSType(), QString::number(m_iId+1)));
                          break;
                      default:
                          graphRect->setPen(QPen(QColor(0xEF7528), 1*m_dCurrentScale, Qt::SolidLine));
                          textRoi->setBrush(QBrush(QColor(0xEF7528)));
                          m_sId = getSType();//QString("RP%1").arg(QString::number(m_iId+1));
                          textRoi->setText(QString("%1 %2").arg(getSType(), QString::number(m_iId+1)));
                          break;
                      }
                      graphRect->resetTransform();
                      graphRect->setPos(nodeFirst->pos());
                      graphRect->setTransformOriginPoint((event.x() - nodeFirst->pos().x())/2, (event.y() - nodeFirst->pos().y())/2);
                      graphRect->setRotation(180);
                      m_scene->addItem(graphRect);
                      m_scene->addItem(nodeFirst);
                      textRoi->setFont(QFont("Arial", 8*m_dCurrentScale));
                      textRoi->setPos(event.x()-((textRoi->boundingRect().width()+2))*m_dCurrentScale, event.y()-((textRoi->boundingRect().height()+2))*m_dCurrentScale);
                      m_scene->addItem(textRoi);
              
                      m_bFirstNodeExist = true;
                  } else if(m_bFirstNodeExist && m_bExists){
                      if(graphRect->isSelected() || textRoi->isSelected()){
                          nodeFirst->setSelected(true);
                          nodeSecond->setSelected(true);
                          textRoi->setSelected(true);
                          graphRect->setSelected(true);
                      }
              
                      if(nodeFirst->isSelected() && nodeSecond->isSelected() && textRoi->isSelected() && graphRect->isSelected())
                          m_bSelected = true;
                      else
                          m_bSelected = false;
                  }
                  //Radio de los nodos en función de la anchura y altura de las etiquetas.
              //    double dRadius = std::max(1.5, std::min(abs(graphRect->rect().width()), abs(graphRect->rect().height()))*0.05);
              //    nodeFirst->setNodeRadius(1/dRadius);
              //    nodeSecond->setNodeRadius(1/dRadius);
              }
              
              

              MOVE:

              void CRoi::move(QPointF event, QPointF limitTopLeft, QPointF limitBottomRight){
                  if(!m_bExists && m_bFirstNodeExist){
                      nodeSecond->setCursor(Qt::ClosedHandCursor);
              
                      if(event.x() < limitTopLeft.x()){
                          nodeSecond->setPos(limitTopLeft.x(), event.y());
                      }
                      if(event.x() > limitBottomRight.x()){
                          nodeSecond->setPos(limitBottomRight.x(), event.y());
                      }
                      if(event.x() > limitTopLeft.x() && event.x() < limitBottomRight.x()) {
                          nodeSecond->setPos(event.x(), event.y());
                      }
              
                      if(event.y() < limitTopLeft.y()){
                          nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                      }
                      if(event.y() > limitBottomRight.y()){
                          nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                      }
                      if(event.y() > limitTopLeft.y() && event.y() > limitBottomRight.y()){
                          nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                      }
              
                      /* Comprobamos qué tipo de roi vamos a dibujar */
                      if (nodeFirst->pos().x() < event.x() && nodeFirst->pos().y() < event.y()) {/* ROI TIPO 1 */
                          textRoi->setPos(nodeFirst->pos().x() - ((textRoi->boundingRect().width()+2))*m_dCurrentScale, nodeFirst->pos().y() - ((textRoi->boundingRect().height()+2))*m_dCurrentScale);
                      } else if (nodeFirst->pos().x() < event.x() && nodeFirst->pos().y() > event.y()) {/* ROI TIPO 2 */
                          textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                      } else if (nodeFirst->pos().x() > event.x() && nodeFirst->pos().y() > event.y()) {/* ROI TIPO 3 */
                          textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                      } else if (nodeFirst->pos().x() > event.x() && nodeFirst->pos().y() < event.y()) {/* ROI TIPO 4 */
                          textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                      }
              
                      graphRect->resetTransform();
                      graphRect->setPos(nodeFirst->pos().x(),nodeFirst->pos().y());
                      graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                      graphRect->setRotation(180);
                      graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
                      punto.setX(nodeFirst->pos().x());
                      punto.setY(nodeFirst->pos().y());
              
              //        graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
              //        graphRect->setRotation(180);
              //        graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
                  } else{
                      if(nodeFirst->isSelected() && !m_bSelected){
                          /* 1. Seleccionamos el nodo con el que vamos a trabajar y asignamos posicion */
                          nodeFirst->setCursor(Qt::ClosedHandCursor);
                          if(event.x() < limitTopLeft.x()){
                              nodeFirst->setPos(limitTopLeft.x(), event.y());
                          }
                          if(event.x() > limitBottomRight.x()){
                              nodeFirst->setPos(limitBottomRight.x(), event.y());
                          }
                          if(event.x() > limitTopLeft.x() && event.x() < limitBottomRight.x()) {
                              nodeFirst->setPos(event.x(), event.y());
                          }
              
                          if(event.y() < limitTopLeft.y()){
                              nodeFirst->setPos(nodeFirst->pos().x(), limitTopLeft.y());
                          }
                          if(event.y() > limitBottomRight.y()){
                              nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                          }
                          if(event.y() > limitTopLeft.y() && event.y() > limitBottomRight.y()){
                              nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                          }
              
                          /* 2. Dibujamos el rectángulo con los nuevos datos */
              //            graphRect->setRect(globalIncX, globalIncY, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
              
                          /* 3. Colocamos el texto */
                          if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {/* ROI TIPO 1 */
                              textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                          } else if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {/* ROI TIPO 2 */
                              textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                          } else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {/* ROI TIPO 3 */
                              textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                          } else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {/* ROI TIPO 4 */
                              textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                          }
                      }
                      if(nodeSecond->isSelected() && !m_bSelected){
                          /* 1. Seleccionamos el nodo con el que vamos a trabajar y asignamos posicion */
                          nodeSecond->setCursor(Qt::ClosedHandCursor);
                          if(event.x() < limitTopLeft.x()){
                              nodeSecond->setPos(limitTopLeft.x(), event.y());
                          }
                          if(event.x() > limitBottomRight.x()){
                              nodeSecond->setPos(limitBottomRight.x(), event.y());
                          }
                          if(event.x() > limitTopLeft.x() && event.x() < limitBottomRight.x()) {
                              nodeSecond->setPos(event.x(), event.y());
                          }
              
                          if(event.y() < limitTopLeft.y()){
                              nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                          }
                          if(event.y() > limitBottomRight.y()){
                              nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                          }
                          if(event.y() > limitTopLeft.y() && event.y() > limitBottomRight.y()){
                              nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                          }
              
              
                          /* 2. Tranformamos el rectangulo y colocamos el texto */
              //            graphRect->resetTransform();
              //            graphRect->setPos(nodeFirst->pos().x(),nodeFirst->pos().y());
              //            graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
              //            graphRect->setRotation(180);
              //            graphRect->setRect(0,0, nodeSecond->pos().<x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
              //            punto.setX(nodeFirst->pos().x());
              //            punto.setY(nodeFirst->pos().y());
              //            globalIncX = punto.x()-nodeFirst->pos().x();
              //            globalIncY = punto.y()-nodeFirst->pos().y();
              //            graphRect->setRect(globalIncX, globalIncY, nodeSecond->pos().x()-nodeFirst->pos().x(), nodeSecond->pos().y()-nodeFirst->pos().y());
                          //graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                          //graphRect->setRotation(180);
                          /* Si quiero convertirla en una ROI TIPO 1 */
                          if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {
                              textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                          }
                          /* Si quiero convertirla en una ROI TIPO 2 */
                          else if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {
                              textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                          }
                          /* Si quiero convertirla en una ROI TIPO 3 */
                          else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {
                              textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                          }
                          /* Si quiero convertirla en una ROI TIPO 4 */
                          else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {
                              textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                          }
              
                          /* Dibujamos el rectángulo con los nuevos datos */
              //            globalIncX = punto.x()-nodeFirst->pos().x();
              //            globalIncY = punto.y()-nodeFirst->pos().y();
              //            graphRect->setRect(globalIncX, globalIncY, nodeSecond->pos().x()-nodeFirst->pos().x(), nodeSecond->pos().y()-nodeFirst->pos().y());
                      }
                      if(m_bSelected){
                          nodeFirst->setCursor(Qt::ClosedHandCursor);
                          nodeSecond->setCursor(Qt::ClosedHandCursor);
                          textRoi->setCursor(Qt::ClosedHandCursor);
                          graphRect->setCursor(Qt::ClosedHandCursor);
              
                          if(nodeFirst->pos().x() < limitTopLeft.x()){
                              nodeFirst->setPos(limitTopLeft.x(), nodeFirst->pos().y());
                          }
                          if(nodeFirst->pos().x() > limitBottomRight.x()){
                              nodeFirst->setPos(limitBottomRight.x(), nodeFirst->pos().y());
                          }
              
                          if(nodeFirst->pos().y() < limitTopLeft.y()){
                              nodeFirst->setPos(nodeFirst->pos().x(), limitTopLeft.y());
                          }
                          if(nodeFirst->pos().y() > limitBottomRight.y()){
                              nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                          }
              
                          if(nodeSecond->pos().x() < limitTopLeft.x()){
                              nodeSecond->setPos(limitTopLeft.x(), nodeSecond->pos().y());
                          }
                          if(nodeSecond->pos().x() > limitBottomRight.x()){
                              nodeSecond->setPos(limitBottomRight.x(), nodeSecond->pos().y());
                          }
              
                          if(nodeSecond->pos().y() < limitTopLeft.y()){
                              nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                          }
                          if(nodeSecond->pos().y() > limitBottomRight.y()){
                              nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                          }
              
                          /* Si quiero convertirla en una ROI TIPO 1 */
                          if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {
                              textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                          }
                          /* Si quiero convertirla en una ROI TIPO 2 */
                          else if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {
                              textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                          }
                          /* Si quiero convertirla en una ROI TIPO 3 */
                          else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {
                              textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                          }
                          /* Si quiero convertirla en una ROI TIPO 4 */
                          else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {
                              textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                          }
                      }
                      graphRect->resetTransform();
                      graphRect->setPos(nodeFirst->pos().x(),nodeFirst->pos().y());
                      graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                      graphRect->setRotation(180);
                      graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
              //        m_scene->addItem(graphRect);
                      punto.setX(nodeFirst->pos().x());
                      punto.setY(nodeFirst->pos().y());
                  }
              
                  //Radio de los nodos en función de la anchura y altura de las etiquetas.
              //    double dRadius = std::max(1.5, std::min(abs(graphRect->rect().width()), abs(graphRect->rect().height()))*0.05);
              //    nodeFirst->setNodeRadius(1/dRadius);
              //    nodeSecond->setNodeRadius(1/dRadius);
              //    m_scene->addItem(graphRect);
              //    m_scene->addItem(nodeFirst);
              //    m_scene->addItem(nodeSecond);
              //    m_scene->addItem(textRoi);
              }
              

              RELEASE:

              void CRoi::release(QPointF event, QPointF limitTopLeft, QPointF limitBottomRight)
              {
                  if(!m_bExists && m_bFirstNodeExist){
                      if(event.x() < limitTopLeft.x()){
                          nodeSecond->setPos(limitTopLeft.x(), event.y());
                      }
                      if(event.x() > limitBottomRight.x()){
                          nodeSecond->setPos(limitBottomRight.x(), event.y());
                      }
                      if(event.x() > limitTopLeft.x() && event.x() < limitBottomRight.x()) {
                          nodeSecond->setPos(event.x(), event.y());
                      }
              
                      if(event.y() < limitTopLeft.y()){
                          nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                      }
                      if(event.y() > limitBottomRight.y()){
                          nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                      }
                      if(event.y() > limitTopLeft.y() && event.y() > limitBottomRight.y()){
                          nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                      }
              //        m_scene->addItem(nodeSecond);
                      nodeSecond->setCursor(Qt::CrossCursor);
              
                      /* Comprobamos qué tipo de roi vamos a dibujar */
                      if (nodeFirst->pos().x() < event.x() && nodeFirst->pos().y() < event.y()) {/* ROI TIPO 1 */
                          textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                      } else if (nodeFirst->pos().x() < event.x() && nodeFirst->pos().y() > event.y()) {/* ROI TIPO 2 */
                          textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                      } else if (nodeFirst->pos().x() > event.x() && nodeFirst->pos().y() > event.y()) {/* ROI TIPO 3 */
                          textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                      } else if (nodeFirst->pos().x() > event.x() && nodeFirst->pos().y() < event.y()) {/* ROI TIPO 4 */
                          textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                      }
              
                      graphRect->setPen(QPen(QColor(0xFF871B), 1*m_dCurrentScale, Qt::SolidLine));
                      graphRect->resetTransform();
                      graphRect->setPos(nodeFirst->pos().x(), nodeFirst->pos().y());
                      graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                      graphRect->setRotation(180);
                      graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
              //        m_scene->addItem(graphRect);
              
                      punto.setX(nodeFirst->pos().x());
                      punto.setY(nodeFirst->pos().y());
                      m_bExists = true;
              
                  } else{
              
                      if(nodeFirst->pos().x() < limitTopLeft.x()){
                          nodeFirst->setPos(limitTopLeft.x(), nodeFirst->pos().y());
                      }
                      if(nodeFirst->pos().x() > limitBottomRight.x()){
                          nodeFirst->setPos(limitBottomRight.x(), nodeFirst->pos().y());
                      }
              
                      if(nodeFirst->pos().y() < limitTopLeft.y()){
                          nodeFirst->setPos(nodeFirst->pos().x(), limitTopLeft.y());
                      }
                      if(nodeFirst->pos().y() > limitBottomRight.y()){
                          nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                      }
              
              
                      if(nodeSecond->pos().x() < limitTopLeft.x()){
                          nodeSecond->setPos(limitTopLeft.x(), nodeSecond->pos().y());
                      }
                      if(nodeSecond->pos().x() > limitBottomRight.x()){
                          nodeSecond->setPos(limitBottomRight.x(), nodeSecond->pos().y());
                      }
              
                      if(nodeSecond->pos().y() < limitTopLeft.y()){
                          nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                      }
                      if(nodeSecond->pos().y() > limitBottomRight.y()){
                          nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                      }
              
              
              
                      if(nodeFirst->isSelected() && !m_bSelected){
                          nodeFirst->setCursor(Qt::OpenHandCursor);
                          nodeFirst->setSelected(false);
                      }
                      if(nodeSecond->isSelected() && !m_bSelected){
                          nodeSecond->setCursor(Qt::CrossCursor);
                          nodeSecond->setSelected(false);
                      }
                      if(m_bSelected){
                          nodeFirst->setCursor(Qt::CrossCursor);
                          nodeSecond->setCursor(Qt::CrossCursor);
                          textRoi->setCursor(Qt::CrossCursor);
                          graphRect->setCursor(Qt::CrossCursor);
                          /**** Descomentar para deseleccionar al soltar ****/
                          //nodeFirst->setSelected(false);
                          //nodeSecond->setSelected(false);
                          //textRoi->setSelected(false);
                          //graphRect->setSelected(false);
                      }
                      graphRect->resetTransform();
                      graphRect->setPos(nodeFirst->pos().x(),nodeFirst->pos().y());
                      graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                      graphRect->setRotation(180);
                      graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
              //        m_scene->addItem(graphRect);
                      punto.setX(nodeFirst->pos().x());
                      punto.setY(nodeFirst->pos().y());
                  }
              
                  m_scene->addItem(graphRect);
                  m_scene->addItem(nodeFirst);
                  m_scene->addItem(nodeSecond);
                  m_scene->addItem(textRoi);
              
                  calculateVertices();
                  //Radio de los nodos en función de la anchura y altura de las etiquetas.
              //    double dRadius = std::max(1.5, std::min(abs(graphRect->rect().width()), abs(graphRect->rect().height()))*0.05);
              //    nodeFirst->setNodeRadius(1/dRadius);
              //    nodeSecond->setNodeRadius(1/dRadius);
              }
              

              Sorry @JonB, yes, I use a QGraphicsRectItem

              JonBJ 1 Reply Last reply
              0
              • ivanicyI ivanicy

                It's a very long class, but here are the important functions:

                Variables:

                    QGraphicsScene *m_scene;
                    int id;
                    CNode* nodeFirst;
                    CNode* nodeSecond;
                    QGraphicsRectItem *graphRect;
                    QGraphicsSimpleTextItem *textRoi;
                    bool m_bExists;
                    bool m_bFirstNodeExist;
                    bool m_bSelected;
                    CNode* nodeMax;
                    CNode* nodeMin;
                    int m_iId;
                    int m_iType;
                    int m_iTarget;
                    QString m_sId;
                    ROIType m_RoiType;
                    QPointF m_topLeft, m_topRight, m_bottomLeft, m_bottomRight;
                    int globalIncX, globalIncY;
                    QPoint punto;
                    QString m_sPathNames;
                    double m_dCurrentScale;
                

                PRESS:

                void CRoi::press(QPointF event, QPointF limitTopLeft, QPointF limitBottomRight){
                    if(!m_bFirstNodeExist){
                        nodeFirst->setPos(event.x(), event.y());
                        if(nodeFirst->pos().x() < limitTopLeft.x()){
                //            nodeFirst->setPos(limitTopLeft.x(), nodeFirst->pos().y());
                            nodeFirst->setX(limitTopLeft.x());
                        }
                        if(nodeFirst->pos().x() > limitBottomRight.x()){
                //            nodeFirst->setPos(limitBottomRight.x(), nodeFirst->pos().y());
                            nodeFirst->setX(limitBottomRight.x());
                        }
                
                        if(nodeFirst->pos().y() < limitTopLeft.y()){
                //            nodeFirst->setPos(nodeFirst->pos().x(), limitTopLeft.y());
                            nodeFirst->setY(limitTopLeft.y());
                        }
                        if(nodeFirst->pos().y() > limitBottomRight.y()){
                //            nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                            nodeFirst->setY(limitBottomRight.y());
                        }
                
                        switch (m_iType) {
                        case 0:
                //            graphRect->setPen(QPen(QColor(0x0000FF), 1, Qt::SolidLine));
                //            textRoi->setBrush(QBrush(QColor(0x0000FF)));
                            graphRect->setPen(QPen(QColor(0xFF871B), 1*m_dCurrentScale, Qt::SolidLine));
                            textRoi->setBrush(QBrush(QColor(0xFF871B)));
                            m_sId = getSType();//QString("RP%1").arg(QString::number(m_iId+1));
                            textRoi->setText(QString("%1 %2").arg(getSType(), QString::number(m_iId+1)));
                            break;
                        case 1:
                            graphRect->setPen(QPen(QColor(0xFF0000), 1*m_dCurrentScale, Qt::SolidLine));
                            textRoi->setBrush(QBrush(QColor(0xFF0000)));
                            m_sId = getSType();//QString("RP%1").arg(QString::number(m_iId+1));
                            textRoi->setText(QString("%1 %2").arg(getSType(), QString::number(m_iId+1)));
                            break;
                        case 2:
                            graphRect->setPen(QPen(QColor(0xFFFB00), 1*m_dCurrentScale, Qt::SolidLine));
                            textRoi->setBrush(QBrush(QColor(0xFFFB00)));
                            m_sId = getSType();//QString("RP%1").arg(QString::number(m_iId+1));
                            textRoi->setText(QString("%1 %2").arg(getSType(), QString::number(m_iId+1)));
                            break;
                        default:
                            graphRect->setPen(QPen(QColor(0xEF7528), 1*m_dCurrentScale, Qt::SolidLine));
                            textRoi->setBrush(QBrush(QColor(0xEF7528)));
                            m_sId = getSType();//QString("RP%1").arg(QString::number(m_iId+1));
                            textRoi->setText(QString("%1 %2").arg(getSType(), QString::number(m_iId+1)));
                            break;
                        }
                        graphRect->resetTransform();
                        graphRect->setPos(nodeFirst->pos());
                        graphRect->setTransformOriginPoint((event.x() - nodeFirst->pos().x())/2, (event.y() - nodeFirst->pos().y())/2);
                        graphRect->setRotation(180);
                        m_scene->addItem(graphRect);
                        m_scene->addItem(nodeFirst);
                        textRoi->setFont(QFont("Arial", 8*m_dCurrentScale));
                        textRoi->setPos(event.x()-((textRoi->boundingRect().width()+2))*m_dCurrentScale, event.y()-((textRoi->boundingRect().height()+2))*m_dCurrentScale);
                        m_scene->addItem(textRoi);
                
                        m_bFirstNodeExist = true;
                    } else if(m_bFirstNodeExist && m_bExists){
                        if(graphRect->isSelected() || textRoi->isSelected()){
                            nodeFirst->setSelected(true);
                            nodeSecond->setSelected(true);
                            textRoi->setSelected(true);
                            graphRect->setSelected(true);
                        }
                
                        if(nodeFirst->isSelected() && nodeSecond->isSelected() && textRoi->isSelected() && graphRect->isSelected())
                            m_bSelected = true;
                        else
                            m_bSelected = false;
                    }
                    //Radio de los nodos en función de la anchura y altura de las etiquetas.
                //    double dRadius = std::max(1.5, std::min(abs(graphRect->rect().width()), abs(graphRect->rect().height()))*0.05);
                //    nodeFirst->setNodeRadius(1/dRadius);
                //    nodeSecond->setNodeRadius(1/dRadius);
                }
                
                

                MOVE:

                void CRoi::move(QPointF event, QPointF limitTopLeft, QPointF limitBottomRight){
                    if(!m_bExists && m_bFirstNodeExist){
                        nodeSecond->setCursor(Qt::ClosedHandCursor);
                
                        if(event.x() < limitTopLeft.x()){
                            nodeSecond->setPos(limitTopLeft.x(), event.y());
                        }
                        if(event.x() > limitBottomRight.x()){
                            nodeSecond->setPos(limitBottomRight.x(), event.y());
                        }
                        if(event.x() > limitTopLeft.x() && event.x() < limitBottomRight.x()) {
                            nodeSecond->setPos(event.x(), event.y());
                        }
                
                        if(event.y() < limitTopLeft.y()){
                            nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                        }
                        if(event.y() > limitBottomRight.y()){
                            nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                        }
                        if(event.y() > limitTopLeft.y() && event.y() > limitBottomRight.y()){
                            nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                        }
                
                        /* Comprobamos qué tipo de roi vamos a dibujar */
                        if (nodeFirst->pos().x() < event.x() && nodeFirst->pos().y() < event.y()) {/* ROI TIPO 1 */
                            textRoi->setPos(nodeFirst->pos().x() - ((textRoi->boundingRect().width()+2))*m_dCurrentScale, nodeFirst->pos().y() - ((textRoi->boundingRect().height()+2))*m_dCurrentScale);
                        } else if (nodeFirst->pos().x() < event.x() && nodeFirst->pos().y() > event.y()) {/* ROI TIPO 2 */
                            textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                        } else if (nodeFirst->pos().x() > event.x() && nodeFirst->pos().y() > event.y()) {/* ROI TIPO 3 */
                            textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                        } else if (nodeFirst->pos().x() > event.x() && nodeFirst->pos().y() < event.y()) {/* ROI TIPO 4 */
                            textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                        }
                
                        graphRect->resetTransform();
                        graphRect->setPos(nodeFirst->pos().x(),nodeFirst->pos().y());
                        graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                        graphRect->setRotation(180);
                        graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
                        punto.setX(nodeFirst->pos().x());
                        punto.setY(nodeFirst->pos().y());
                
                //        graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                //        graphRect->setRotation(180);
                //        graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
                    } else{
                        if(nodeFirst->isSelected() && !m_bSelected){
                            /* 1. Seleccionamos el nodo con el que vamos a trabajar y asignamos posicion */
                            nodeFirst->setCursor(Qt::ClosedHandCursor);
                            if(event.x() < limitTopLeft.x()){
                                nodeFirst->setPos(limitTopLeft.x(), event.y());
                            }
                            if(event.x() > limitBottomRight.x()){
                                nodeFirst->setPos(limitBottomRight.x(), event.y());
                            }
                            if(event.x() > limitTopLeft.x() && event.x() < limitBottomRight.x()) {
                                nodeFirst->setPos(event.x(), event.y());
                            }
                
                            if(event.y() < limitTopLeft.y()){
                                nodeFirst->setPos(nodeFirst->pos().x(), limitTopLeft.y());
                            }
                            if(event.y() > limitBottomRight.y()){
                                nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                            }
                            if(event.y() > limitTopLeft.y() && event.y() > limitBottomRight.y()){
                                nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                            }
                
                            /* 2. Dibujamos el rectángulo con los nuevos datos */
                //            graphRect->setRect(globalIncX, globalIncY, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
                
                            /* 3. Colocamos el texto */
                            if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {/* ROI TIPO 1 */
                                textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                            } else if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {/* ROI TIPO 2 */
                                textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                            } else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {/* ROI TIPO 3 */
                                textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                            } else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {/* ROI TIPO 4 */
                                textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                            }
                        }
                        if(nodeSecond->isSelected() && !m_bSelected){
                            /* 1. Seleccionamos el nodo con el que vamos a trabajar y asignamos posicion */
                            nodeSecond->setCursor(Qt::ClosedHandCursor);
                            if(event.x() < limitTopLeft.x()){
                                nodeSecond->setPos(limitTopLeft.x(), event.y());
                            }
                            if(event.x() > limitBottomRight.x()){
                                nodeSecond->setPos(limitBottomRight.x(), event.y());
                            }
                            if(event.x() > limitTopLeft.x() && event.x() < limitBottomRight.x()) {
                                nodeSecond->setPos(event.x(), event.y());
                            }
                
                            if(event.y() < limitTopLeft.y()){
                                nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                            }
                            if(event.y() > limitBottomRight.y()){
                                nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                            }
                            if(event.y() > limitTopLeft.y() && event.y() > limitBottomRight.y()){
                                nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                            }
                
                
                            /* 2. Tranformamos el rectangulo y colocamos el texto */
                //            graphRect->resetTransform();
                //            graphRect->setPos(nodeFirst->pos().x(),nodeFirst->pos().y());
                //            graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                //            graphRect->setRotation(180);
                //            graphRect->setRect(0,0, nodeSecond->pos().<x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
                //            punto.setX(nodeFirst->pos().x());
                //            punto.setY(nodeFirst->pos().y());
                //            globalIncX = punto.x()-nodeFirst->pos().x();
                //            globalIncY = punto.y()-nodeFirst->pos().y();
                //            graphRect->setRect(globalIncX, globalIncY, nodeSecond->pos().x()-nodeFirst->pos().x(), nodeSecond->pos().y()-nodeFirst->pos().y());
                            //graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                            //graphRect->setRotation(180);
                            /* Si quiero convertirla en una ROI TIPO 1 */
                            if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {
                                textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                            }
                            /* Si quiero convertirla en una ROI TIPO 2 */
                            else if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {
                                textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                            }
                            /* Si quiero convertirla en una ROI TIPO 3 */
                            else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {
                                textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                            }
                            /* Si quiero convertirla en una ROI TIPO 4 */
                            else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {
                                textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                            }
                
                            /* Dibujamos el rectángulo con los nuevos datos */
                //            globalIncX = punto.x()-nodeFirst->pos().x();
                //            globalIncY = punto.y()-nodeFirst->pos().y();
                //            graphRect->setRect(globalIncX, globalIncY, nodeSecond->pos().x()-nodeFirst->pos().x(), nodeSecond->pos().y()-nodeFirst->pos().y());
                        }
                        if(m_bSelected){
                            nodeFirst->setCursor(Qt::ClosedHandCursor);
                            nodeSecond->setCursor(Qt::ClosedHandCursor);
                            textRoi->setCursor(Qt::ClosedHandCursor);
                            graphRect->setCursor(Qt::ClosedHandCursor);
                
                            if(nodeFirst->pos().x() < limitTopLeft.x()){
                                nodeFirst->setPos(limitTopLeft.x(), nodeFirst->pos().y());
                            }
                            if(nodeFirst->pos().x() > limitBottomRight.x()){
                                nodeFirst->setPos(limitBottomRight.x(), nodeFirst->pos().y());
                            }
                
                            if(nodeFirst->pos().y() < limitTopLeft.y()){
                                nodeFirst->setPos(nodeFirst->pos().x(), limitTopLeft.y());
                            }
                            if(nodeFirst->pos().y() > limitBottomRight.y()){
                                nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                            }
                
                            if(nodeSecond->pos().x() < limitTopLeft.x()){
                                nodeSecond->setPos(limitTopLeft.x(), nodeSecond->pos().y());
                            }
                            if(nodeSecond->pos().x() > limitBottomRight.x()){
                                nodeSecond->setPos(limitBottomRight.x(), nodeSecond->pos().y());
                            }
                
                            if(nodeSecond->pos().y() < limitTopLeft.y()){
                                nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                            }
                            if(nodeSecond->pos().y() > limitBottomRight.y()){
                                nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                            }
                
                            /* Si quiero convertirla en una ROI TIPO 1 */
                            if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {
                                textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                            }
                            /* Si quiero convertirla en una ROI TIPO 2 */
                            else if (nodeSecond->pos().x() > nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {
                                textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                            }
                            /* Si quiero convertirla en una ROI TIPO 3 */
                            else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() < nodeFirst->pos().y()) {
                                textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                            }
                            /* Si quiero convertirla en una ROI TIPO 4 */
                            else if (nodeSecond->pos().x() < nodeFirst->pos().x() && nodeSecond->pos().y() > nodeFirst->pos().y()) {
                                textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                            }
                        }
                        graphRect->resetTransform();
                        graphRect->setPos(nodeFirst->pos().x(),nodeFirst->pos().y());
                        graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                        graphRect->setRotation(180);
                        graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
                //        m_scene->addItem(graphRect);
                        punto.setX(nodeFirst->pos().x());
                        punto.setY(nodeFirst->pos().y());
                    }
                
                    //Radio de los nodos en función de la anchura y altura de las etiquetas.
                //    double dRadius = std::max(1.5, std::min(abs(graphRect->rect().width()), abs(graphRect->rect().height()))*0.05);
                //    nodeFirst->setNodeRadius(1/dRadius);
                //    nodeSecond->setNodeRadius(1/dRadius);
                //    m_scene->addItem(graphRect);
                //    m_scene->addItem(nodeFirst);
                //    m_scene->addItem(nodeSecond);
                //    m_scene->addItem(textRoi);
                }
                

                RELEASE:

                void CRoi::release(QPointF event, QPointF limitTopLeft, QPointF limitBottomRight)
                {
                    if(!m_bExists && m_bFirstNodeExist){
                        if(event.x() < limitTopLeft.x()){
                            nodeSecond->setPos(limitTopLeft.x(), event.y());
                        }
                        if(event.x() > limitBottomRight.x()){
                            nodeSecond->setPos(limitBottomRight.x(), event.y());
                        }
                        if(event.x() > limitTopLeft.x() && event.x() < limitBottomRight.x()) {
                            nodeSecond->setPos(event.x(), event.y());
                        }
                
                        if(event.y() < limitTopLeft.y()){
                            nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                        }
                        if(event.y() > limitBottomRight.y()){
                            nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                        }
                        if(event.y() > limitTopLeft.y() && event.y() > limitBottomRight.y()){
                            nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                        }
                //        m_scene->addItem(nodeSecond);
                        nodeSecond->setCursor(Qt::CrossCursor);
                
                        /* Comprobamos qué tipo de roi vamos a dibujar */
                        if (nodeFirst->pos().x() < event.x() && nodeFirst->pos().y() < event.y()) {/* ROI TIPO 1 */
                            textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                        } else if (nodeFirst->pos().x() < event.x() && nodeFirst->pos().y() > event.y()) {/* ROI TIPO 2 */
                            textRoi->setPos(nodeFirst->pos().x() - (textRoi->boundingRect().width()+2)*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                        } else if (nodeFirst->pos().x() > event.x() && nodeFirst->pos().y() > event.y()) {/* ROI TIPO 3 */
                            textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() + 2*m_dCurrentScale);
                        } else if (nodeFirst->pos().x() > event.x() && nodeFirst->pos().y() < event.y()) {/* ROI TIPO 4 */
                            textRoi->setPos(nodeFirst->pos().x() + 2*m_dCurrentScale, nodeFirst->pos().y() - (textRoi->boundingRect().height()+2)*m_dCurrentScale);
                        }
                
                        graphRect->setPen(QPen(QColor(0xFF871B), 1*m_dCurrentScale, Qt::SolidLine));
                        graphRect->resetTransform();
                        graphRect->setPos(nodeFirst->pos().x(), nodeFirst->pos().y());
                        graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                        graphRect->setRotation(180);
                        graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
                //        m_scene->addItem(graphRect);
                
                        punto.setX(nodeFirst->pos().x());
                        punto.setY(nodeFirst->pos().y());
                        m_bExists = true;
                
                    } else{
                
                        if(nodeFirst->pos().x() < limitTopLeft.x()){
                            nodeFirst->setPos(limitTopLeft.x(), nodeFirst->pos().y());
                        }
                        if(nodeFirst->pos().x() > limitBottomRight.x()){
                            nodeFirst->setPos(limitBottomRight.x(), nodeFirst->pos().y());
                        }
                
                        if(nodeFirst->pos().y() < limitTopLeft.y()){
                            nodeFirst->setPos(nodeFirst->pos().x(), limitTopLeft.y());
                        }
                        if(nodeFirst->pos().y() > limitBottomRight.y()){
                            nodeFirst->setPos(nodeFirst->pos().x(), limitBottomRight.y());
                        }
                
                
                        if(nodeSecond->pos().x() < limitTopLeft.x()){
                            nodeSecond->setPos(limitTopLeft.x(), nodeSecond->pos().y());
                        }
                        if(nodeSecond->pos().x() > limitBottomRight.x()){
                            nodeSecond->setPos(limitBottomRight.x(), nodeSecond->pos().y());
                        }
                
                        if(nodeSecond->pos().y() < limitTopLeft.y()){
                            nodeSecond->setPos(nodeSecond->pos().x(), limitTopLeft.y());
                        }
                        if(nodeSecond->pos().y() > limitBottomRight.y()){
                            nodeSecond->setPos(nodeSecond->pos().x(), limitBottomRight.y());
                        }
                
                
                
                        if(nodeFirst->isSelected() && !m_bSelected){
                            nodeFirst->setCursor(Qt::OpenHandCursor);
                            nodeFirst->setSelected(false);
                        }
                        if(nodeSecond->isSelected() && !m_bSelected){
                            nodeSecond->setCursor(Qt::CrossCursor);
                            nodeSecond->setSelected(false);
                        }
                        if(m_bSelected){
                            nodeFirst->setCursor(Qt::CrossCursor);
                            nodeSecond->setCursor(Qt::CrossCursor);
                            textRoi->setCursor(Qt::CrossCursor);
                            graphRect->setCursor(Qt::CrossCursor);
                            /**** Descomentar para deseleccionar al soltar ****/
                            //nodeFirst->setSelected(false);
                            //nodeSecond->setSelected(false);
                            //textRoi->setSelected(false);
                            //graphRect->setSelected(false);
                        }
                        graphRect->resetTransform();
                        graphRect->setPos(nodeFirst->pos().x(),nodeFirst->pos().y());
                        graphRect->setTransformOriginPoint((nodeSecond->pos().x() - nodeFirst->pos().x())/2, (nodeSecond->pos().y() - nodeFirst->pos().y())/2);
                        graphRect->setRotation(180);
                        graphRect->setRect(0,0, nodeSecond->pos().x() - nodeFirst->pos().x(), nodeSecond->pos().y() - nodeFirst->pos().y());
                //        m_scene->addItem(graphRect);
                        punto.setX(nodeFirst->pos().x());
                        punto.setY(nodeFirst->pos().y());
                    }
                
                    m_scene->addItem(graphRect);
                    m_scene->addItem(nodeFirst);
                    m_scene->addItem(nodeSecond);
                    m_scene->addItem(textRoi);
                
                    calculateVertices();
                    //Radio de los nodos en función de la anchura y altura de las etiquetas.
                //    double dRadius = std::max(1.5, std::min(abs(graphRect->rect().width()), abs(graphRect->rect().height()))*0.05);
                //    nodeFirst->setNodeRadius(1/dRadius);
                //    nodeSecond->setNodeRadius(1/dRadius);
                }
                

                Sorry @JonB, yes, I use a QGraphicsRectItem

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @ivanicy said in Problem drawing in QGraphicsView:

                Sorry @JonB, yes, I use a QGraphicsRectItem

                Then I don't know what is causing the artefacts to be left.

                1 Reply Last reply
                0
                • ivanicyI Offline
                  ivanicyI Offline
                  ivanicy
                  wrote on last edited by
                  #8

                  I found a solution. It was simple. I had to add scene->update() in move() function to fix it.

                  1 Reply Last reply
                  1

                  • Login

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