PDA

View Full Version : Qgraphicsview zoom issue



tarunrajsingh
19th April 2013, 03:14
Iam using mouse wheel to zoom my view,I have a condition not to zoom out scene below original.
But when i zoom out to original size I dont see original size ,some part is not visible in view port.

When I debugged I found that if I zoom out from a zoomed scene ,just before it reaches scene's original size
my factor becomes 0.9999 so my condition fails and it doesn't zoom out anymore.

Can anybody point our for me if I am doing something wrong here.




void MyView::wheelEvent(QWheelEvent* event)
{

qreal delta=pow((double)2,(event->delta())/ 120.0);
qreal factor = matrix().scale(delta, delta).mapRect(QRectF(0, 0, 1, 1)).width();

if (factor < 1.000 || factor > 100)
return;

this->scale(delta,delta);
}

Santosh Reddy
19th April 2013, 12:38
Try this


void MyView::wheelEvent(QWheelEvent* event)
{
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));
}

tarunrajsingh
22nd April 2013, 04:03
works like a charm:-)

tarunrajsingh
23rd April 2013, 06:11
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.

Santosh Reddy
23rd April 2013, 11:08
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.


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();
}

tarunrajsingh
23rd April 2013, 12:09
Thanks ,I also need zoomed Rect in scene coordinates,Is there a way to get it from WheelEvent delta?

Santosh Reddy
23rd April 2013, 12:28
take the sceneRect() and apply the transformation on the rectagle manually.