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. QGraphicsItem Moving and crop

QGraphicsItem Moving and crop

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.2k Views
  • 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.
  • L Offline
    L Offline
    lietak
    wrote on last edited by
    #1

    Hello,

    I'm working with some QGraphicsSvgItems

    Currently I have set every item in place.

    Last feature is to move some of them when some event triggers.

    I can now move the Items I want with:
    svgItem->moveBy(0, 50);

    Now the item looks funny after it goes down by 50 and overlaps the item underneath.
    I would like to crop the bottom of the item by 50.

    I tried:
    svgItem->boundingRect().setBottom(-50);
    It doesn't seem to have any effect.
    Also tried several other functions which doesn't seem to do anything either.

    While browsing the documentation of scenes, transformations and boundingrects etc. I'm getting a bit confused.

    What is the best way to crop away part of the Item?

    thanks.

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

      @lietak said in QGraphicsItem Moving and crop:

      svgItem->boundingRect().setBottom(-50);

      svgItem->boundingRect().setBottom(-50);
      

      That really doesn't have any effect: You get a copy of the boundingRect, then change that copy. The result is not used, and therefore discarded. If you use the standard QGraphicsSvgItem, you don't control it's boundingRect - the item itself decides how big it is.
      One way around:

      • Create your own QGraphicsItem-derived class where you can fully control the boundingRect - let's call it ClipperItem
      • Set the flag QGraphicsItem::ItemClipsChildrenToShape
      • Make the QGraphicsSvgItem a child of a ClipperItem

      Adjust the ClipperItem's boundingRect as needed to show only the part of your Svg you want to show

      Edit:
      For a quick tryout, you can use QGraphicsWidget and use setGeometry to define it's size and position. However, for better performance (and if you don't need all the other features of QGraphicsWidget), I would implement a QGraphicsItem-derived class as final solution.

      1 Reply Last reply
      3
      • L lietak

        Hello,

        I'm working with some QGraphicsSvgItems

        Currently I have set every item in place.

        Last feature is to move some of them when some event triggers.

        I can now move the Items I want with:
        svgItem->moveBy(0, 50);

        Now the item looks funny after it goes down by 50 and overlaps the item underneath.
        I would like to crop the bottom of the item by 50.

        I tried:
        svgItem->boundingRect().setBottom(-50);
        It doesn't seem to have any effect.
        Also tried several other functions which doesn't seem to do anything either.

        While browsing the documentation of scenes, transformations and boundingrects etc. I'm getting a bit confused.

        What is the best way to crop away part of the Item?

        thanks.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @lietak
        For real clipping you can subclass QGraphicsSvgItem:

        class MyGraphicsSvgItem : public QGraphicsSvgItem
        {
        public:
             MyGraphicsSvgItem(QGraphicsItem* parent = Q_NULLPTR) : QGraphicsSvgItem(parent)
             {
              this->setFlag( QGraphicsItem::ItemClipsToShape );
            }
        
            virtual QPainterPath shape() const Q_DECL_OVERRIDE {
                QPainterPath s = QGraphicsSvgItem::shape();
                QRectF clipRect = this->boundingRect();
                      clipRect.setTop( clipRect.bottom()-50 );
                QPainterPath clipPath;
                      clipPath.addRect(  clipRect );
                 s = s.subtracted( clipPath );
                return s;
           }
        };
        

        (untested)

        --- 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
        3
        • L Offline
          L Offline
          lietak
          wrote on last edited by lietak
          #4

          Ok thanks for the replies and help.

          I kinda knew I would probably had to subclass it. Was mostly wondering if there was some other way.

          So I'll try it out.

          Edit.
          I used @raven-worx solution
          It works. Awesome.
          Just made -50 as variable so I can change it per item :)

          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