Re: Qgraphicsview zoom issue
Try this
Code:
{
static qreal factor = 1.0;
const qreal delta = event->delta() / 120.0;
if(delta < 0)
factor--;
else if(delta > 0)
factor++;
factor = qBound(1.0, factor, 100.0);
this->setTransform(QTransform(factor, 0, 0, factor, 0, 0));
}
Re: Qgraphicsview zoom issue
Re: Qgraphicsview zoom issue
One more question when you set transform for zooming (0,0) remains at top left always and the zoomed scene goes away from origin...I even set the anchorundermouse flag but still getting the same behaviour.I want my scene to zoom proportionally from all sides and also I have constraint that scene size should be equal to view size initially.
Re: Qgraphicsview zoom issue
To apply transformation wrt to centre use QGraphicsView::AnchorViewCenter as transformatiojn anchor.
To fit the scene to the view initially use QGraphicsView::fitInView()
Just an exmaple, which was I used some time earlier.
Code:
{
public:
protected:
{
if(mView != 0)
if(event
->type
() == QEvent::Wheel) {
const QWheelEvent * wheelEvent
= static_cast<QWheelEvent
*>
(event
);
if(qApp->keyboardModifiers() & Qt::ControlModifier)
{
const int steps = wheelEvent->delta() / 120;
static const double scaleFactor = 1.0;
static const qreal minFactor = 1.0;
static const qreal maxFactor = 100.0;
if(steps > 0)
mH += scaleFactor;
else
mH -= scaleFactor;
mH = qBound(minFactor, mH, maxFactor);
mView->setTransformationAnchor(mView->AnchorUnderMouse);
mView->setTransform(QTransform(mH, 0.0, 0.0, mH, 0, 0));
return true;
}
}
return parent()->eventFilter(object, event);
}
private:
qreal mH;
};
int main(int argc, char* argv[])
{
// Add a rectangle and a text item
view.setScene(&scene);
view.installEventFilter(new Zoomer(&view));
view.fitInView(scene.sceneRect(), Qt::KeepAspectRatioByExpanding);
view.showMaximized();
return app.exec();
}
Re: Qgraphicsview zoom issue
Thanks ,I also need zoomed Rect in scene coordinates,Is there a way to get it from WheelEvent delta?
Re: Qgraphicsview zoom issue
take the sceneRect() and apply the transformation on the rectagle manually.