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. Smooth zoom with fitInView

Smooth zoom with fitInView

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.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.
  • W Offline
    W Offline
    Waf29
    wrote on last edited by
    #1

    Hi,

    I have a QGraphicsView and a QGraphicsScene. In my QGraphicsScene I have QGraphicsItems.
    I want to zoom on some of these QGraphicsItems when clicking on a QPushButton. It's working with the fitInView function, but i want a smooth zoom. How can I do it ?

    Thank you !

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Santosh Reddy
      wrote on last edited by
      #2

      Smooth zoom? do mean to slowly, kind of animated zoom? If yes then you can implement the mouse wheel event of QGraphicsView and and then set the transformation of the view as required.

      SS

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Waf29
        wrote on last edited by
        #3

        Yes, I mean an animated zoom (sorry for my english).
        I have already implement the mouse wheel event of QGraphicsView, and I can have an animated zoom.
        I'm using "scale" function, and a QTimeLine to do it.

        But what i want is to zoom on my QGraphicsItem, when I click on my QPushButton.
        Currently I'm using this :

        @ this->myview->fitInView(this->myscene->items().at(0),Qt::KeepAspectRatio);@

        But it is not animated. I thought to use a QTimeLine again, but I don't know how to implement it with the "fitInView" function.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Santosh Reddy
          wrote on last edited by
          #4

          It would be somthing like this

          @class ItemZoomer : public QObject
          {
          Q_OBJECT
          public:
          explicit ItemZoomer(QGraphicsView * view, const QGraphicsItem * item, int time, QObject * parent = 0)
          : QObject(parent)
          , mView(view)
          , mItem(item)
          , mHeight(0)
          , mWidth(0)
          {
          QTimeLine * height = new QTimeLine(time, this);
          QTimeLine * width = new QTimeLine(time, this);

              QRectF start = mView->sceneRect();
              QRectF end = mItem->mapToScene(mItem->boundingRect()).boundingRect();
          
              height->setFrameRange(start.height(), end.height());
              width->setFrameRange(start.width(), end.width());
          
              connect(height, SIGNAL(frameChanged(int)), SLOT(setHeight(int)));
              connect(width, SIGNAL(frameChanged(int)), SLOT(setWidth(int)));
          
              height->start();
              width->start();
          }
          

          private slots:
          void setHeight(int height) { mHeight = height, update(); }
          void setWidth(int width) { mWidth = width, update(); }

          private:
          QGraphicsView * mView;
          const QGraphicsItem * mItem;
          int mHeight, mWidth;

          void update(void)
          {
              QRectF rect = mItem->mapToScene(mItem->boundingRect()).boundingRect();
              rect.setHeight(mHeight);
              rect.setWidth(mWidth);
              mView->fitInView(rect);
          }
          

          };@

          Adjust the start and end rectangles

          SS

          1 Reply Last reply
          0
          • W Offline
            W Offline
            Waf29
            wrote on last edited by
            #5

            That is what i wanted to do and it's working !
            Thank you !

            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