Qt編程之通過鼠標滾輪事件縮放QGraphicsView里面的Item


 

 

首先自己subclass QGraphicsView的一個類,叫DiagramView,然后重新實現它的滾輪事件函數,然后發送一個縮放信號:

 1 oid DiagramView::wheelEvent(QWheelEvent * event){
 2 
 3     if (event->delta() > 0)
 4     {
 5         emit mouseWheelZoom(true);
 6     }
 7     else
 8     {
 9         emit mouseWheelZoom(false);
10     }
11 
12 }
View Code

 

然后用connect把這個信號連接到要實現的槽函數上(ScaleFactor初始化為30):

 1 void TopologyEditor::Zoom(bool zoom){
 2 
 3     
 4 
 5     
 6 
 7     if (zoom && scaleFactor >= 0)
 8     {
 9         
10         scaleFactor += 10;
11         QMatrix old_matrix;
12         old_matrix = view->matrix();
13         view->resetMatrix();
14         view->translate(old_matrix.dx(), old_matrix.dy());
15         view->scale(scaleFactor/100.0, scaleFactor/100.0);
16     }
17     else if (!zoom && scaleFactor >= 0)
18     {
19         scaleFactor -= 10;
20         QMatrix old_matrix;
21         old_matrix = view->matrix();
22         view->resetMatrix();
23         view->translate(old_matrix.dx(), old_matrix.dy());
24         
25         view->scale( scaleFactor/100.0,  scaleFactor/100.0);
26 
27     }
28 
29     else if (scaleFactor < 0){
30     
31         scaleFactor = 0.0;
32     }
33 
34 }
View Code

 

 

 

references:

http://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel

http://www.qtcentre.org/threads/52603-Zoom-effect-by-mouse-Wheel-in-QGraphicsview

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM