Smooth zoom with fitInView

  • 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 !

  • 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.

  • 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.

  • It would be somthing like this

    @class ItemZoomer : public QObject
    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)));

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

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

    void update(void)
        QRectF rect = mItem->mapToScene(mItem->boundingRect()).boundingRect();


    Adjust the start and end rectangles

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.