c++ - QCustomPlot QCPItemEllipse 位置

标签 c++ qt qcustomplot

我有一个以 (0,0) 为中心的 QCustomPlot 图表,我想在图表上的所有数据周围添加一个椭圆项目,但我只能设置椭圆的左上角和右下角位置,这几乎显示在图表的中心,但它有点偏离,因为我无法将中心设置为椭圆的中心,因为中心不是 anchor 的位置

有谁知道如何将椭圆项目的中心设置为 (0,0)?

    QCPItemEllipse *ellipse = new QCPItemEllipse(ui->customPlot);
    ellipse->setAntialiased(true);
    ellipse->topLeft->setCoords(lowestX, highestY);
    ellipse->bottomRight->setCoords(highestX, lowestY);

最佳答案

伙计,椭圆中心的位置是由它的左上点和右下点决定的。

例如center.x == (topLeft.x + bottomRight.x)/2center.y == (topLeft.y + bottomRight.y)/2。这就是省略号在数学中的作用:)。

在您的代码中,如果 (lowestX + hightX) == 0 & (lowestY + hightY) == 0,中心将位于 (0,0)。

或者我没听懂你在说什么?

如果您想以简单的方式移动椭圆的中心,您可以创建派生自 QCPItemEllipse 的类,其中包含字段 QCPItemTracer *mCenterTracer 并在构造函数中包含以下内容:

mCenterTracer->setStyle(QCPItemTracer::tsNone);

topLeft->setParentAnchor(mCenterTracer->position);
bottomRight->setParentAnchor(mCenterTracer->position);

topLeft->setCoords(-xRadius/2.0, -yRadius/2.0);
bottomRight->setCoords(xRadius/2.0, yRadius/2.0);

因此移动中心(不改变半径)的方法如下所示:

void FreakingEllipse::moveCenter(double x, double y) {
    mCenterTracer->position->setCoords(x, y);
}

顺便说一句,如果您希望椭圆具有以像素为单位的恒定半径,同时它的中心仍然坚持绘制坐标,您可以添加以下内容:

topLeft->setType(QCPItemPosition::ptAbsolute);
bottomRight->setType(QCPItemPosition::ptAbsolute);

关于c++ - QCustomPlot QCPItemEllipse 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19632468/

相关文章:

.net - 文件系统观察者

c++ - 为什么我在播放 mp3 歌曲时收到错误提示 : bitstream problem, 重新同步跳过?

c++ - 如何在线程中更新 QCustomPlot 数据?

c++ - 更改刻度间隔以使用一小时增量?

c++ - Node-expat 错误的 ELF 类 : ELFCLASS64

c++ - 管理单个字符串的内存

c++ - QWebEnginePage 中的透明背景

c++ - 从 C++ 向 QML 公开串口名称

QTextEdit设置固定行高、段落间距

c++ - 在 QT 应用程序(VS 插件)中使用 QcustomPlot 基本代码停止工作程序