fol*_*bis 4 c++ qt qt5 qtcharts
在我的应用程序中,我QChart
用来显示折线图。不幸的是,Qt Charts 不支持使用鼠标滚轮缩放和通过鼠标滚动等基本功能。是的,有橡皮筋功能,但仍然不支持滚动等,这对用户来说不是那么直观。另外我只需要缩放 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);
}
}
Run Code Online (Sandbox Code Playgroud)
这有效,但放大然后缩小不会导致相同的结果。有一个小的偏差。调试后我发现chart()->plotArea()
总是返回相同的矩形,所以这个解决方法没用。
有没有办法只获得可见区域的矩形?或者可能有人可以为我指出正确的解决方案,如何通过鼠标为 QChartView 进行缩放/滚动?
而不是使用zoomIn()
,zoomOut()
你可以使用zoom()
如下所示:
class ChartView : public QChartView {
protected:
void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE
{
qreal factor = event->angleDelta().y() > 0? 0.5: 2.0;
chart()->zoom(factor);
event->accept();
QChartView::wheelEvent(event);
}
};
Run Code Online (Sandbox Code Playgroud)
关于zoomIn()
and zoomOut()
,不清楚它指的是什么坐标,我还在投资,等我有更多信息我会更新我的答案。
更新:
正如我观察到的问题之一是浮点的乘法,另一个是定位图形的中心,为了没有这些问题,我的解决方案重置缩放然后设置更改:
class ChartView : public QChartView {
qreal mFactor=1.0;
protected:
void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE
{
chart()->zoomReset();
mFactor *= event->angleDelta().y() > 0 ? 0.5 : 2;
QRectF rect = chart()->plotArea();
QPointF c = chart()->plotArea().center();
rect.setWidth(mFactor*rect.width());
rect.moveCenter(c);
chart()->zoomIn(rect);
QChartView::wheelEvent(event);
}
};
Run Code Online (Sandbox Code Playgroud)