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. How to refresh QGraphicsItem bounding rectangle after scaling?

How to refresh QGraphicsItem bounding rectangle after scaling?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 1.9k 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.
  • clostridium_difficileC Offline
    clostridium_difficileC Offline
    clostridium_difficile
    wrote on last edited by
    #1

    In my application I scale the item, but bounding rectangle does not change. How to make bounding rectangle change when scaling an item?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      How did you implement it ?

      1 Reply Last reply
      0
      • clostridium_difficileC Offline
        clostridium_difficileC Offline
        clostridium_difficile
        wrote on last edited by
        #3
        void ScaleTool::Update(QPointF currentPoint)
        {
        qreal currentDistance = std::sqrt(std::pow(rect().topLeft().x() - currentPoint.x(), 2) + std::pow(rect().topLeft().y() - currentPoint.y(), 2));
        layer->setScale(currentDistance / startDistance);
        
        setRect(layer->boundingRect());
        setPos(layer->pos());
        }
        

        ScaleTool is derrived from QGraphicsRectItem. I expect line setRect(layer->boundingRect()); to set rectangle to fit layer's bounding rectangle, but it does not change (i checked it in qDebug).

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          Normally you override the virtual function and return the rect

          QRectF CircleItem::boundingRect() const
          {
              qreal penWidth = 1;
              return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
                            diameter + penWidth, diameter + penWidth);
          }
          

          so does setRect(layer->boundingRect());
          set a rect you then return in that boundingRect?

          1 Reply Last reply
          0
          • clostridium_difficileC Offline
            clostridium_difficileC Offline
            clostridium_difficile
            wrote on last edited by
            #5

            Okay, I forgot to reimplement bounding rectangle. But, how can I "refresh" it once it changes?

            mrjjM 1 Reply Last reply
            0
            • clostridium_difficileC clostridium_difficile

              Okay, I forgot to reimplement bounding rectangle. But, how can I "refresh" it once it changes?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @clostridium_difficile
              well could you not just return QRectF QGraphicsRectItem::rect() const
              since you set it in Update ?

              QRectF ScaleTool::boundingRect() const
              {  
                  return rect();
              }
              

              However, i would check what the base class (QGraphicsRectItem) actually returns
              as it might already return rect and what is missing is a call to
              https://doc.qt.io/qt-5/qgraphicsitem.html#prepareGeometryChange

              1 Reply Last reply
              1
              • clostridium_difficileC Offline
                clostridium_difficileC Offline
                clostridium_difficile
                wrote on last edited by
                #7

                solution to my problem was returning mapped to scene bounding rectangle:

                QRectF ImageLayer::boundingRect() const
                {
                	return mapRectToScene(rect());
                }
                
                mrjjM 1 Reply Last reply
                1
                • clostridium_difficileC clostridium_difficile

                  solution to my problem was returning mapped to scene bounding rectangle:

                  QRectF ImageLayer::boundingRect() const
                  {
                  	return mapRectToScene(rect());
                  }
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @clostridium_difficile
                  ah yes good catch. boundingRect is ofc in expected to be in scene coordinates.

                  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