代码之家  ›  专栏  ›  技术社区  ›  user7431005

QPieSlice的Qt坐标

  •  0
  • user7431005  · 技术社区  · 7 年前

    我怎样才能得到QPieSlice(中心)的坐标?

    背景: 我想加一个“标注” see here 我的QChart显示一个qpiseries。 我已将悬停信号连接到自定义插槽。在这个插槽中,我可以访问QPieSlice。 要显示我的“标注”,我需要在我的QChart坐标框中的x和y坐标。“Callout”的锚应该是我当前悬停的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));
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Mailerdaimon    7 年前

    你还得把半径也算进去。

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

    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