在我的应用程序中,我使用QChart显示线条图。不幸的是,Qt不支持使用鼠标轮进行缩放和鼠标滚动等基本功能。是的,有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()总是返回相同的rect,所以这个解决方法是无用的。
有什么方法只可以让我们看到一个可见区域吗?或者有人可以为我指出正确的解决方案,如何通过鼠标对QChartView进行缩放/滚动?
发布于 2018-02-05 16:09:15
与使用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);
}
};关于zoomIn()和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);
}
};发布于 2018-07-02 20:19:13
我通过以下代码实现了x和y缩放的工作:
void wheelEvent(QWheelEvent *event){
qreal factor;
if ( event->delta() > 0 )
factor = 2.0;
else
factor = 0.5;
QRectF r = QRectF(chart()->plotArea().left(),chart()->plotArea().top(),
chart()->plotArea().width()/factor,chart()->plotArea().height()/factor);
QPointF mousePos = mapFromGlobal(QCursor::pos());
r.moveCenter(mousePos);
chart()->zoomIn(r);
QPointF delta = chart()->plotArea().center() -mousePos;
chart()->scroll(delta.x(),-delta.y());}https://stackoverflow.com/questions/48623595
复制相似问题