最近使用qwt的缩放操作遇到一些问题。那就是我使用了QwtPlotZoomer作缩放操作,而QwtPlotZoomer默认右键为还原操作。而我的程序里,右键会弹出菜单。所以就
会有矛盾。于是查遍网络,查遍代码,终于让我找到了解决办法。
让我们先看看QwtPlotZoomer的类注释:
/*
\brief QwtPlotZoomer provides stacked zooming for a plot widget
QwtPlotZoomer selects rectangles from user inputs ( mouse or keyboard )
translates them into plot coordinates and adjusts the axes to them.
The selection is supported by a rubber band and optionally by displaying
the coordinates of the current mouse position.
Zooming can be repeated as often as possible, limited only by
maxStackDepth() or minZoomSize(). Each rectangle is pushed on a stack.
The default setting how to select rectangles is
a QwtPickerDragRectMachine with the following bindings:
To traverse the zoom stack the following bindings are used:
- QwtEventPattern::MouseSelect3, QwtEventPattern::KeyUndo\n
Zoom out one position on the zoom stack
- QwtEventPattern::MouseSelect6, QwtEventPattern::KeyRedo\
Zoom in one position on the zoom stack
- QwtEventPattern::MouseSelect2, QwtEventPattern::KeyHome\n
Zoom to the zoom base
...
*/
大致意思是QwtPlotZoomer提供了一个基于栈式的存储控件。可以通过用户输入(鼠标或键盘)来选择矩形区域进行缩放。每次缩放操作的矩形都会填入栈中。缩放次数不限,除非设置最大堆栈深度或最小缩放尺寸。可以采用如下绑定方式进行堆栈遍历操作。
QwtEventPattern::MouseSelect3 Undo操作,即向堆栈底部遍历
QwtEventPattern::MouseSelect6 Redo操作,即向堆栈顶部遍历
QwtEventPattern::MouseSelect2 Home操作,即直接回到栈底操作
举个例子来说明下:
QwtPlotZoomer缩放采用栈式存储各次缩放操作,如下:
zoom base
|
V
一次缩放
|
V
二次缩放
|
V
三次缩放
|
V
四次缩放
|
V
......
假设现在处于四次缩放,那么如何回到三次缩放?如果在三次缩放,又如何回到四次缩放?如何直接回到zoom base呢?
这就相当于Undo,Redo和Home操作。
// 默认使用鼠标左键缩放,右键还原
QwtPlotZoomer *zoomer = new QwtPlotZoomer( canvas() );
// 中键 Undo操作
zoomer->setMousePattern(QwtEventPattern::MouseSelect3, Qt::MidButton );
// 中键 + Ctrl Redo操作
zoomer->setMousePattern(QwtEventPattern::MouseSelect6, Qt::MidButton, Qt::ControlModifier );
// 中键 + Shift zoom base,即Home操作
zoomer->setMousePattern(QwtEventPattern::MouseSelect2, Qt::MidButton, Qt::ShiftModifier );
zoomer->setEnabled(true);<span style="font-family:Courier New;background-color: rgb(240, 240, 240);">
// 这样鼠标右键就不会进行Undo操作了,而鼠标中键则会进行Undo。</span>