使用QCPItemTracer

时间:2015-05-08 11:18:18

标签: c++ qt labels qcustomplot

我现在尝试更长时间来创建一种机制,在坐标的图上我的点旁边创建文本标签。从文档中,我已经读过我需要使用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);

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

1 个答案:

答案 0 :(得分:0)

我设法让标签正常工作:

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;
}