在QChartView的轴系统中获取鼠标坐标 [英] Get mouse coordinates in QChartView's axis system

查看:1538
本文介绍了在QChartView的轴系统中获取鼠标坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 QChartView 的绘图区域上获取鼠标坐标?优选地以轴为单位。目的是显示鼠标坐标,同时在绘图上移动鼠标,以便用户可以测量绘图对象。

Is there a way to get mouse's coordinates on plotting area of a QChartView? Preferably in the axis units. The goal is to display mouse's coordinates while moving the mouse around on the plot so the user can measure plotted objects.

我在此上找不到任何内置函数 QChartView ,所以我尝试使用 QChartView :: mouseMoveEvent(QMouseEvent * event)尝试计算结果在绘图区域中的位置。问题是我无法获得绘图区坐标系的任何参考。
我尝试使用 mapToScene mapToItem mapToParent 以及所有对象上的 mapFrom ... 反向,我都可以尝试尝试执行此操作,但无济于事。

I couldn't find any built in function for this on QChartView, so I'm trying to use QChartView::mouseMoveEvent(QMouseEvent *event) to try and calculate the resulting position in the plotting area. The problem is I can't get any reference to the plot area's coordinate system. I've tried using mapToScene, mapToItem and mapToParent and also the reverse mapFrom... on all objects I can grab a hold of to try to do this, but to no avail.

我发现 QChartView :: chart-> childItems()[2] 确实是绘图区域,不包括轴和轴标签。然后,我可以调用 QChartView :: chart-> childItems()[2]-> setCursor(Qt :: CrossCursor)以使十字仅出现在绘图区域而不是在相邻的对象上。但是,尽管如此,我尝试的任何事情似乎都未能正确引用该对象的坐标系。

I've found that QChartView::chart->childItems()[2] is indeed the plotting area, excluding the axis and axis labels. I can then call QChartView::chart->childItems()[2]->setCursor(Qt::CrossCursor) to make a cross appear only on the plotting area and not on the adjacent objects. But still, nothing I try seems to make a correct reference to this object's coordinate system.

推荐答案

QChartView 只是带有嵌入式 scene() QGraphicsView 。要在任何图表中获取坐标,您必须进行几次坐标转换:

QChartView is simply a QGraphicsView with an embedded scene(). To get coordinates within any of the charts, you have to go through several coordinate transformations:


  1. 从视图小部件坐标开始

  2. view-> mapToScene :小部件(视图)坐标→场景坐标

  3. chart-> mapFromScene :场景坐标→图表项坐标

  4. chart-> mapToValue :图表项目坐标→给定系列中的值。

  5. 以给定系列中的值坐标结尾。

  1. Start with the view widget coordinates
  2. view->mapToScene: widget (view) coordinates → scene coordinates
  3. chart->mapFromScene: scene coordinates → chart item coordinates
  4. chart->mapToValue: chart item coordinates → value in a given series.
  5. End with value coordinates in a given series.

术语图表项目和图表小部件是同义词,因为 QChart is-a QGraphicsWidget is -a QGraphicsItem 。请注意, QGraphicsWidget 不是不是一个 QWidget

The term "chart item" and "chart widget" are synonyms, since QChart is-a QGraphicsWidget is-a QGraphicsItem. Note that QGraphicsWidget is not a QWidget!

像这样运行它就像一个饰物(感谢Marcel!):

Implementing it like this works like a charm (thanks, Marcel!):

auto const widgetPos = event->localPos();
auto const scenePos = mapToScene(QPoint(static_cast<int>(widgetPos.x()), static_cast<int>(widgetPos.y()))); 
auto const chartItemPos = chart()->mapFromScene(scenePos); 
auto const valueGivenSeries = chart()->mapToValue(chartItemPos); 
qDebug() << "widgetPos:" << widgetPos; 
qDebug() << "scenePos:" << scenePos; 
qDebug() << "chartItemPos:" << chartItemPos; 
qDebug() << "valSeries:" << valueGivenSeries;

这篇关于在QChartView的轴系统中获取鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆