在我的应用程序中,我QChart
用来显示折线图。不幸的是,Qt Charts 不支持使用鼠标滚轮进行缩放和鼠标滚动等基本功能。是的,有 RubberBand 功能,但仍然不支持滚动等对用户来说不太直观的端。此外,我只需要缩放 x 轴,某种setRubberBand(QChartView::HorizontalRubberBand)
但使用鼠标滚轮。到目前为止,在深入研究后,QChartView
我使用了以下解决方法:
class ChartView : public QChartView {
protected:
void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE
{
QRectF rect = chart()->plotArea();
if(event->angleDelta().y() > 0)
{
rect.setX(rect.x() + rect.width() / 4);
rect.setWidth(rect.width() / 2);
}
else
{
qreal adjustment = rect.width() / 2;
rect.adjust(-adjustment, 0, adjustment, 0);
}
chart()->zoomIn(rect);
event->accept();
QChartView::wheelEvent(event);
}
}
这可行,但放大然后缩小不会导致相同的结果。有一个小的偏差。调试后我发现chart()->plotArea()
总是返回相同的矩形,所以这个解决方法是没用的。
有什么方法可以只得到一个可见区域的矩形吗?或者可能有人可以指出正确的解决方案如何通过鼠标为 QChartView 进行缩放/滚动?