Very old thread and I'm not the OP, but I finally got around to implementing this, so I'll leave it here in case anyone is looking for an example.
I handled it with a QwtPlotZoomer (m_zoomer) and update the zoom() based on the mouse movement while the middle mouse button is down. It's quite fast, even on large plots and is immune to changes in the plot size.
I store the mouse drag location (m_mouseDragLocation) when the middle button is first pressed and then update it after each 1px movement during a drag operation.
Then, inside QwtPlot::eventFilter()
// MouseMove only fires while a mouse button is pressed
if (event
->type
() == QEvent::MouseMove) {
// User wants to pan the plot
if (m_middleMouseDown)
{
// Get current zoom rectangle
QRectF zoomRect
= m_zoomer
->zoomRect
();
// Convert the change in position for the last drag (1px) from
// pixel space to plot space.
double deltaX
= invTransform
(QwtPlot::xBottom, m_mouseDragLocation.
x()) - invTransform
(QwtPlot::xBottom, cursor
().
pos().
x());
double deltaY
= invTransform
(QwtPlot::yLeft, m_mouseDragLocation.
y()) - invTransform
(QwtPlot::yLeft, cursor
().
pos().
y());
// Update the zoom rect with the new values
zoomRect.setX(zoomRect.x() + deltaX);
zoomRect.setY(zoomRect.y() + deltaY);
zoomRect.setWidth (zoomRect.width() + deltaX);
zoomRect.setHeight(zoomRect.height() + deltaY);
// Store for continued dragging
m_mouseDragLocation = cursor().pos();
// Set the zoom to its new position
m_zoomer->zoom(zoomRect);
}
}
// MouseMove only fires while a mouse button is pressed
if (event->type() == QEvent::MouseMove)
{
// User wants to pan the plot
if (m_middleMouseDown)
{
// Get current zoom rectangle
QRectF zoomRect = m_zoomer->zoomRect();
// Convert the change in position for the last drag (1px) from
// pixel space to plot space.
double deltaX = invTransform(QwtPlot::xBottom, m_mouseDragLocation.x()) - invTransform(QwtPlot::xBottom, cursor().pos().x());
double deltaY = invTransform(QwtPlot::yLeft, m_mouseDragLocation.y()) - invTransform(QwtPlot::yLeft, cursor().pos().y());
// Update the zoom rect with the new values
zoomRect.setX(zoomRect.x() + deltaX);
zoomRect.setY(zoomRect.y() + deltaY);
zoomRect.setWidth (zoomRect.width() + deltaX);
zoomRect.setHeight(zoomRect.height() + deltaY);
// Store for continued dragging
m_mouseDragLocation = cursor().pos();
// Set the zoom to its new position
m_zoomer->zoom(zoomRect);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks