使用QCPItemTracer在QCustomPlot中显示点

时间:2022-07-25 18:46:11

I am trying for a longer while now to create a mechanism that would create text labels next to my points on a plot with the coordinates. From the documentation, I have read that I need to use QCPItemTracer for that. No matter how i try, I cannot display any additional items on my plot using this object. In the QCustomPlot examples, there is one program that user QCPItemTracer, but when i run it, I also dont see any additional objects. I am trying to run the example code from bellow:

我现在尝试更长时间来创建一个机制,在坐标图上的点旁边创建文本标签。从文档中,我已经读过我需要使用QCPItemTracer。无论我如何尝试,我都无法使用此对象在我的绘图上显示任何其他项目。在QCustomPlot示例中,有一个用户QCPItemTracer的程序,但是当我运行它时,我也没有看到任何其他对象。我试图从下面运行示例代码:

QCPItemTracer *phaseTracer = new QCPItemTracer(customPlot);
customPlot->addItem(phaseTracer);
phaseTracer->setGraph(customPlot->graph(DATA_PLOT));
phaseTracer->setGraphKey(7);
phaseTracer->setInterpolating(true);
phaseTracer->setStyle(QCPItemTracer::tsCircle);
phaseTracer->setPen(QPen(Qt::red));
phaseTracer->setBrush(Qt::red);
phaseTracer->setSize(7);

From my understanding this was supposed to add red circles on my plot points. It does not. I Would really aprichiate any help in this matter, some example code maybe. I am struggling with this for a really long time.

根据我的理解,这应该在我的情节点上添加红色圆圈。它不是。我真的会在这件事上提供任何帮助,也许是一些示例代码。我很长时间都在努力解决这个问题。

1 个解决方案

#1


I have managed to get the labels working:

我设法使标签工作:

returnCodes_t PlotData::insertPointLabel(const int& index, const double& x, const double& y)
{
    QCPItemText *textLabel = new QCPItemText(m_parentPlot);
    m_parentPlot->addItem(textLabel);
    textLabel->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter);
    textLabel->position->setType(QCPItemPosition::ptPlotCoords);
    textLabel->position->setCoords(x, y); // place position at center/top of axis rect
    textLabel->setText(QString("x%1 y%2").arg(x).arg(y));
    textLabel->setVisible(labelsVisible);
    m_pointLabels.insert(index, textLabel);

    return return_success;
}

#1


I have managed to get the labels working:

我设法使标签工作:

returnCodes_t PlotData::insertPointLabel(const int& index, const double& x, const double& y)
{
    QCPItemText *textLabel = new QCPItemText(m_parentPlot);
    m_parentPlot->addItem(textLabel);
    textLabel->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter);
    textLabel->position->setType(QCPItemPosition::ptPlotCoords);
    textLabel->position->setCoords(x, y); // place position at center/top of axis rect
    textLabel->setText(QString("x%1 y%2").arg(x).arg(y));
    textLabel->setVisible(labelsVisible);
    m_pointLabels.insert(index, textLabel);

    return return_success;
}