Qt Coordinates of QPieSlice

Qt Coordinates of QPieSlice

本文关键字:QPieSlice of Coordinates Qt      更新时间:2023-10-16

如何获取QPieSlice(中心(的坐标?

背景:我想在显示QPieSeries的QChart中添加一个"标注"。我已将悬停信号连接到自定义插槽。在这个插槽中,我可以访问QPieSlice。为了显示我的"标注",我需要QChart坐标系中的x和y坐标。"标注"的锚应该是我当前悬停的QPieSlice的中心。

如何获取QPieSlice中心的坐标?

我试过类似的东西:

double angle = pieSlice->startAngle()+pieSlice->angleSpan()/2;
double x = 1*cos(angle*deg2rad);
double y = 1*sin(angle*deg2rad);
callout->setAnchor(QPointF(x,y));

您还需要将半径计算在内。

如果你想得到中心,你可以把绘图半径的一半作为公式的输入,得到下面给出的圆上的点:

x = cx + r * cos(a)
y = cy + r * sin(a)

因此,结合您的代码,可以得到:

double angle = pieSlice->startAngle()+pieSlice->angleSpan()/2;
double calloutRadius= plotRadius*0.5;
double x = centerPlotX + calloutRadius*cos(angle*deg2rad);
double y = centerPlotY + calloutRadius*sin(angle*deg2rad);
callout->setAnchor(QPointF(x,y));

其中

plotRadius = Radius of your pie plot
centerPlotX = the X center of your pie plot
centerPlotY = the Y center of your pie plot