{
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();
}
class Zoomer : public QObject
{
public:
explicit Zoomer(QGraphicsView * parent) : QObject(parent), mH(1.0) { }
protected:
bool eventFilter(QObject * object, QEvent * event)
{
QGraphicsView * mView = qobject_cast<QGraphicsView *>(object);
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[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
// Add a rectangle and a text item
(new QGraphicsTextItem("Scene", new QGraphicsRectItem(0, 0, 200, 100, 0, &scene), &scene))->setPos(10, 10);
QGraphicsView view;
view.setScene(&scene);
view.installEventFilter(new Zoomer(&view));
view.fitInView(scene.sceneRect(), Qt::KeepAspectRatioByExpanding);
view.showMaximized();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks