Zoom effect by mouse Wheel in QGraphicsview
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 ..............
Re: Zoom effect by mouse Wheel in QGraphicsview
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.
Re: Zoom effect by mouse Wheel in QGraphicsview
Quote:
Originally Posted by
Santosh Reddy
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::AnchorUnde rMouse);
//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);
Re: Zoom effect by mouse Wheel in QGraphicsview
very hard to read, please add [Code] tags
Added after 10 minutes:
Looks like you are not geting the calculations correct, check the following
Code:
{
// 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::AnchorUnderMouse);
ui->graphicsView->setTransform(QTransform(h11, h12, h21, h22, 0, 0));
}
Re: Zoom effect by mouse Wheel in QGraphicsview
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 ........
Re: Zoom effect by mouse Wheel in QGraphicsview
Did you try this
Code:
ui
->graphicsView
->setTransformationAnchor
(QGraphicsView::AnchorUnderMouse);
Quote:
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.
Re: Zoom effect by mouse Wheel in QGraphicsview
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
Re: Zoom effect by mouse Wheel in QGraphicsview
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).
Re: Zoom effect by mouse Wheel in QGraphicsview
i have all done . but the zoom from mouse pointer is not done, zoom is doing to qgraphics view only
Re: Zoom effect by mouse Wheel in QGraphicsview
See if this workaround works, add a QGraphicsRectItem, and set the QGraphicsRectItem rect larger than the screen (monitor) size
Re: Zoom effect by mouse Wheel in QGraphicsview
you can find the solution in this form
http://www.qtcentre.org/wiki/index.p...ng_and_Zooming
I used it in my code like this :
Code:
//in header file
protected:
//in cpp file
ui
->graphicsView
->setTransformationAnchor
(QGraphicsView::AnchorUnderMouse);
// 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));
}