DEV Community

Cover image for Curve Tracer
王飞
王飞

Posted on

Curve Tracer

By using QCustomplot to draw a tracer on a 2D image, it is possible to draw a single QCPAxisRect.

If using QCPItemStraightLine to draw lines, it is not possible to draw all rows and columns successfully. I hope a skilled technician can provide guidance.

VarCurveTracer::VarCurveTracer(QCustomPlot* customplot, QCPAxisRect* rect, QCPGraph* graph, QColor color, QObject* parent) 
    :QObject(parent)
    ,m_customplot(customplot)
    ,m_rect(rect)
    ,m_graph(graph)
{
    if (!m_customplot) {
        qDebug() << "m_customplot is null.";
        return;
    }

    // 新建跟踪器
    m_tracer = new QCPItemTracer(m_customplot);
    // 绑定轴矩形,超出轴矩形的部分不会显示
    m_tracer->setClipAxisRect(m_rect);
    // 跟踪器样式
    m_tracer->setStyle(QCPItemTracer::tsPlus);
    // 跟踪器的大小
    m_tracer->setSize(20);
    m_tracer->setPen(QPen(Qt::red));
    m_tracer->setBrush(Qt::red);

    m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
    m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);
    // 绑定曲线,只在曲线的数据点上出现
    m_tracer->setGraph(m_graph);

    // 新建数据点位置显示图标
    m_label = new QCPItemText(m_customplot);
    // 绑定轴矩形,超出轴矩形的部分不会显示
    m_label->setClipAxisRect(m_rect);
    m_label->setPadding(QMargins(3, 3, 3, 3));
    // 边框颜色
    m_label->setPen(QPen(Qt::black));
    // 填充颜色
    m_label->setBrush(QBrush(color));
    // 根据颜色的亮度值自动翻转字体颜色
    if (color.lightness() > 80) {
        m_label->setColor(Qt::black);
    }
    else {
        m_label->setColor(Qt::white);
    }
    m_label->setText("");
    m_label->setTextAlignment(Qt::AlignLeft);
    m_label->setPositionAlignment(Qt::AlignRight);
    // 图标位置跟随跟踪器tracer变化
    m_label->position->setParentAnchor(m_tracer->position);

    setVisible(false);
}
Enter fullscreen mode Exit fullscreen mode
class VarCurveTracer : public QObject
{
    Q_OBJECT

public:
    explicit VarCurveTracer(QCustomPlot* customplot, \
        QCPAxisRect* rect, \
        QCPGraph* graph,\
        QColor color, \
        QObject* parent = nullptr);
    ~VarCurveTracer();

    void setVisible(bool on);
    void updateTracerPosition(double key);

private:
    QCustomPlot* m_customplot;
    QCPItemTracer* m_tracer; // 跟踪的点
    QCPItemText* m_label; // 显示的数值
    QCPAxisRect* m_rect;
    QCPGraph* m_graph;
};
Enter fullscreen mode Exit fullscreen mode

Image description

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs