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. Clipping is not working !

Clipping is not working !

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.1k Views 2 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.
  • B Offline
    B Offline
    Bert59
    wrote on last edited by
    #1

    Hi,

    I have made a QGraphicsItem called Cursor. I have reimplemented the paint method and after drawing the item I have added painter->setClipRect(QRectF(0, 0, 100, 100)); The size of the rectangle is just for testing.
    I have also added the same rectangle to the scene to get visual references.
    Unfortunately the cursor remains visible even if I move outside the rectangle.
    391ea3a7-0040-45ea-a428-24f6df46e8cf-image.png
    What could be the reason for this behavior? What did I wrong?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #4

      You are calling setClipRect after everything is already painted. This method does not work like an eraser that will clear everything outside that rectangle. It only sets a clip area for all following painting methods i.e. anything that uses that painter afterwards will not draw outside of that region.
      So for it to do anything useful in this case you must call it before you start drawing any lines.

      1 Reply Last reply
      3
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #2

        It's hard to say without more context but I'm guessing you're calling this inside item's paint() method? The painter coordinates in that method are local to the item so if you set the clip rect like that it's gonna move along with the item. If you want to clip it relatively to the view you'll gonna have to translate that rect from item coordinates to view coordinates, i.e. adjust for item's position in the scene and scene's position in the view.

        1 Reply Last reply
        1
        • B Offline
          B Offline
          Bert59
          wrote on last edited by
          #3

          Thank you for your answer.
          Yes I'm calling clipRect from inside item's paint() method.
          I'm still struggling with this issue.
          If the size of the item is 50x50 and I set painter->setClipRect(QRectF(0, 0, 20, 20)) should I not only see a part of the item?

          Here is the code of the paint method

          void Cursor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
          {
              qreal innerSize = m_size * 0.2;
              if (innerSize>10)
                  innerSize = 10;
              else if (innerSize<5)
                  innerSize=5;
          
              qreal halfInnerSize = innerSize / 2;
              QPen pen;
              pen.setColor(m_color);
              painter->setPen(pen);
              painter->drawRect(QRectF(-halfInnerSize, -halfInnerSize, innerSize, innerSize));
          
              painter->drawLine(QLineF(0, -halfInnerSize, 0, halfInnerSize));
              painter->drawLine(QLineF(-halfInnerSize, 0, halfInnerSize, 0));
              pen.setWidth(penWidth);
              painter->setPen(pen);
              qreal halfSize = m_size/2;
              qreal gap = 0.15*m_size;
              painter->drawLine(QLineF(0, -halfSize, 0, -gap));
              painter->drawLine(QLineF(0, gap, 0, halfSize));
              painter->drawLine(QLineF(-halfSize, 0, -gap, 0));
              painter->drawLine(QLineF(gap, 0, halfSize, 0));
          
              QRectF rect;
              rect.setRect(0, 0, 50, 50);
              QPointF point(mapToScene(0, 0));
              qDebug() << "Point" << point;
              qDebug() << "Scene pos: " << this->scenePos();
              painter->setClipRect(0, 0, 10, 10);
          
          }
          
          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #4

            You are calling setClipRect after everything is already painted. This method does not work like an eraser that will clear everything outside that rectangle. It only sets a clip area for all following painting methods i.e. anything that uses that painter afterwards will not draw outside of that region.
            So for it to do anything useful in this case you must call it before you start drawing any lines.

            1 Reply Last reply
            3
            • B Offline
              B Offline
              Bert59
              wrote on last edited by
              #5

              Thank you for your help.
              If you know how it works under the hood it’s obvious that setClipRect has to be placed first.
              Sometimes the Qt help should be a bit more accurate, especially if you are a beginner.

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #6

                No worries, I'm glad I could help.
                Clipping works like that in pretty much all major drawing APIs. It's the same in GDI, OpenGL and others, not just in Qt.
                I guess nobody just thought that it could be understood differently having some experience with any of them, but if you're a beginner I can see how this could happen.

                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