PDA

View Full Version : Zoom effect by mouse Wheel in QGraphicsview



chirudi
31st December 2012, 08:47
Dear Frds,
In my project i have taken Qgraphicsscene and Qgraphicsview . I want to zoom the graphicsview using mouse wheel from the mouse pointer . I have tried but it not works with me . if i am zooming the view is not zooming from the mouse poiinter...............


anybody can help me ..............

Santosh Reddy
31st December 2012, 09:15
What did you try, show us somthing.

You need to get the scroll steps fomr the wheel event and apply the appripriate transformation on the view.

chirudi
31st December 2012, 12:14
What did you try, show us somthing.

You need to get the scroll steps fomr the wheel event and apply the appripriate transformation on the view.

void Widget::wheelEvent(QWheelEvent* event) {

QPointF pointBeforeScale(ui->graphicsView->mapToScene(event->pos()));

//Get the original screen centerpoint
QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());
ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);
//Scale the view ie. do the zoom
double scaleFactor = 1.15; //How fast we zoom
if(event->delta() > 0) {
//Zoom in
ui->graphicsView->scale(scaleFactor, scaleFactor);
} else {
//Zooming out
ui->graphicsView->scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}
ui->graphicsView->setTransformationAnchor(QGraphicsView::NoAnchor);
//Get the position after scaling, in scene coords
QPointF pointAfterScale(ui->graphicsView->mapToScene(event->pos()));

//Get the offset of how the screen moved
QPointF offset = pointBeforeScale - pointAfterScale;

//Adjust to the new center for correct zooming
QPointF newCenter = screenCenter + offset;
SetCenter(newCenter);

Santosh Reddy
31st December 2012, 12:37
very hard to read, please add
tags

Added after 10 minutes:

Looks like you are not geting the calculations correct, check the following

[CODE]void Widget::wheelEvent(QWheelEvent* event)
{
// Typical Calculations (Ref Qt Doc)
const int degrees = event->delta() / 8;
int steps = degrees / 15;

// Declare below as class member vars and set default values as below
// qreal h11 = 1.0
// qreal h12 = 0
// qreal h21 = 1.0
// qreal h22 = 0

double scaleFactor = 1.0; //How fast we zoom
const qreal minFactor = 1.0;
const qreal maxFactor = 10.0;
if(steps > 0)
{
h11 = (h11 >= maxFactor) ? h11 : (h11 + scaleFactor);
h22 = (h22 >= maxFactor) ? h22 : (h22 + scaleFactor);
}
else
{
h11 = (h11 <= minFactor) ? minFactor : (h11 - scaleFactor);
h22 = (h22 <= minFactor) ? minFactor : (h22 - scaleFactor);
}

ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);
ui->graphicsView->setTransform(QTransform(h11, h12, h21, h22, 0, 0));
}

chirudi
1st January 2013, 04:40
Thank you for reply
In my project i have Qgraphicview and also Qgraphicscene and i have added Qpainterpath in Qgraphicscene and Qgraphicscene is added in Qgraphicsview so i am able to do zoom in graphicsview but the zoom is not done from the mouse pointer but from the center of graphicsview.

And another problem is that while drawing in the graphicsview, the view also move with drawing.
so please help me ............

ok dear i have slove the problem of moving of graphicview while drawing . i have set scenerect of graphicsview so the problem is slove but now while zooming is not done on the mouse pointer ........

Santosh Reddy
1st January 2013, 08:29
Did you try this

ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);

Note that the effect of this property is noticeable when only a part of the scene is visible (i.e., when there are scroll bars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.

chirudi
2nd January 2013, 04:14
That i have tried but then also i am not getting the perfect zoom is there any problem of scenerect i have widget of size 1280*1024. and Graphicsview of size 1141*991 or suggest other tools on which i can draw my object on paintevent and also perform zoom effect on it .........

thank's

Santosh Reddy
2nd January 2013, 06:03
having the scene rect bigger thatn view rect shuold make the Zoom effect work as expected, is there any thing else you are missing.

Other Tools: You can use a normal QWidget and implement paintevent, but you will need to handle zoom effect while painting (will not be as stright forward as in case of QGraphicsView).

chirudi
5th January 2013, 11:40
i have all done . but the zoom from mouse pointer is not done, zoom is doing to qgraphics view only

Santosh Reddy
5th January 2013, 12:03
See if this workaround works, add a QGraphicsRectItem, and set the QGraphicsRectItem rect larger than the screen (monitor) size

alexvista1234
21st June 2014, 08:19
you can find the solution in this form

http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_ Zooming


I used it in my code like this :




//in header file

protected:
virtual void wheelEvent(QWheelEvent* event);

//in cpp file
void Dialog::wheelEvent(QWheelEvent *event){

ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);
// Scale the view / do the zoom
double scaleFactor = 1.15;
if(event->delta() > 0) {
// Zoom in
ui->graphicsView-> scale(scaleFactor, scaleFactor);

} else {
// Zooming out
ui->graphicsView->scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}


//ui->graphicsView->setTransform(QTransform(h11, h12, h21, h22, 0, 0));
}