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.
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.
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
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);
very hard to read, please add [Code] tags
Added after 10 minutes:
Looks like you are not geting the calculations correct, check the following
Qt 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->setTransform(QTransform(h11, h12, h21, h22, 0, 0)); }To copy to clipboard, switch view to plain text mode
Last edited by Santosh Reddy; 31st December 2012 at 12:37.
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
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 ........
Last edited by chirudi; 1st January 2013 at 05:01.
Did you try this
Qt Code:
To copy to clipboard, switch view to plain text modeNote 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.
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
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
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).
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
i have all done . but the zoom from mouse pointer is not done, zoom is doing to qgraphics view only
See if this workaround works, add a QGraphicsRectItem, and set the QGraphicsRectItem rect larger than the screen (monitor) size
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
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 :
Qt Code:
//in header file protected: //in cpp file // 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)); }To copy to clipboard, switch view to plain text mode
Bookmarks