QWT--自定义坐标轴刻度值

一、效果展示

定义xBottom为带单位的刻度值

 

 定义xBottom为分钟:秒数的刻度值

 

 

二、具体步骤

2.1、自定义坐标轴

自定义坐标轴刻度,需要继承自QwtScaleDraw,然后重写如下虚函数

virtual QwtText label(double v) const;

定义一个规则,然后设置坐标轴范围时,按定义的规则传入即可。

例如,定义mm:ss刻度值:

#ifndef TIMESCALEDRAW_H
#define TIMESCALEDRAW_H

#include <QWT/qwt_scale_draw.h>
#include <QDateTime>

class TimeScaleDraw : public QwtScaleDraw
{
public:
    TimeScaleDraw();

    virtual QwtText label(double v) const;
};

#endif // TIMESCALEDRAW_H
#include "timescaledraw.h"

TimeScaleDraw::TimeScaleDraw()
{

}

QwtText TimeScaleDraw::label(double v) const
{
    QDateTime dt;
    dt.setTime_t((int)v);
    return dt.toString("mm:ss");
}

传入的值是v,但是返回的是一个字符串,这个字符串是实际显示在坐标轴上的刻度值,如果需要刻度值带单位,则可以这样写:

QwtText TimeScaleDraw::label(double v) const
{
    QString ret = "";
    ret = QString::number(v)+"mA";
    if(ret.isEmpty())
        return;
    else
        return ret;
}

2.2、使用自定义坐标轴

QWT_Area = new QWidget;
QWT_Area->resize(this->width(),this->height());

m_Qwt1 = new QwtPlot(QWT_Area);
m_Qwt1->resize(this->width(),this->height());
m_Qwt1->setAxisScale(QwtPlot::xBottom,0,10);
m_Qwt1->setAxisScale(QwtPlot::yLeft,0,10);
m_Qwt1->enableAxis(QwtPlot::xBottom,true);
m_Qwt1->enableAxis(QwtPlot::xTop,false);
m_Qwt1->enableAxis(QwtPlot::yLeft,true);
m_Qwt1->enableAxis(QwtPlot::yRight,false);


m_Qwt1->setAxisScaleDraw(QwtPlot::xBottom,new TimeScaleDraw());
m_Qwt1->setAxisScale(QwtPlot::xBottom, QDateTime::currentDateTime().toTime_t() - 100,QDateTime::currentDateTime().toTime_t()); //标尺范围

QWT_Area->show();

 

posted @ 2023-04-07 15:19  vickylinj  阅读(208)  评论(0编辑  收藏  举报