在我的应用程序中,我使用
到目前为止,在深入研究
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进行缩放/滚动的正确解决方案吗?
setRubberBand(QChartView::HorizontalRubberBand)
但使用鼠标滚轮的方式。 - folibisQChartPrivate::zoomIn()
进行了小的更正,即删除高度缩放,但这需要创建自定义类继承自QChart。你的解决方案更加优雅。 - folibis