c++ - Scaling a QGrahicsView when MouseWheenTurned first scrolls, then scales -
i'm trying add zoom in/out functionality graphic i'm drawing in qt. first did extend qgraphicsscene own class graphicsscene , overload wheel event.
class graphicsscene : public qgraphicsscene { q_object public: graphicsscene(qobject *parent, bool drawaxes){ /*drawing stuff here.. */} virtual void wheelevent(qgraphicsscenewheelevent *mouseevent); signals: void mousewheelturned(int); }; void graphicsscene::wheelevent(qgraphicsscenewheelevent* mouseevent) { int numdegrees = mouseevent->delta() / 8; int numsteps = numdegrees / 15; // see qwheelevent documentation emit mousewheelturned(numsteps); }
when wheel turned, event sent view contains scene, , there scale performed:
class graphicsview : public qgraphicsview{ q_object qreal m_currentscale; public: graphicsview(qwidget * parent): qgraphicsview(parent){ m_currentscale = 1.0; } public slots: void onmousewheelturned (int); }; void graphicsview::onmousewheelturned(int steps) { qreal sign = steps>0?1:-1; qreal current = sign* pow(0.05, abs(steps)); if(m_currentscale+current > 0){ m_currentscale += current; qmatrix matrix; matrix.scale(m_currentscale, m_currentscale); this->setmatrix(matrix); } }
this works, noticed if zoom in lot, example top of graphic, graphic no longer in viewport, , zoom out, program first scrolls botton of graphic. can see vertical scrollbar sliding down. when has reached bottom, start zoom out. problem?
i want zoom in/ out without scroll / down behaviour.
you'd better off handling in scene , sending event view.
simply handle event directly in view qgraphicsview::wheelevent, call scale function.
Comments
Post a Comment