QT C++ QCustomPlot 简单使用

发布于:2024-04-30 ⋅ 阅读:(28) ⋅ 点赞:(0)

//本文描述QCustomPlot的简单使用,

//使用QCustomPlot 画曲线分5步

//1.初始化

//2.发送

//3.接收

//4.绑定

//5.重绘

//通常单独的线程发送,发送线程通过emit函数或者wake(WakeOne)函数告诉接收方,

//接收方通常是GUI类的函数,把数据放进vector容器

//定时绑定vector容器和重绘,可以比第2步和第3步慢(因为我们有vector容器装好了的)

//在2、3、4、5步循环往复。

//1.初始化

struct CurveInfo
{
    QString nameX;// X轴label
    QString nameY;// Y轴label
    double minX;//X轴最小
    double minY;//Y轴最小
    double maxX;//X轴最大
    double maxY;//Y轴最大
};

initCurve(QCustomPlot* plot, CurveInfo curveInfo)
{
    plot->setBackground(QBrush(QColor(220, 220, 220)));//背景灰

    plot->addGraph();
    plot->graph(0)->setPen(QPen(Qt::blue));//画笔颜色

    plot->xAxis->setLabel(curveInfo.nameX);//X轴颜色
    plot->yAxis->setLabel(curveInfo.nameY);//Y轴颜色

    // 支持鼠标拖拽轴的范围、滚动缩放轴的范围,左键点选图层(每条曲线独占一个图层)
    plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

    plot->xAxis->setRange(curveInfo.minX, curveInfo.maxX);
    plot->yAxis->setRange(curveInfo.minY, curveInfo.maxY);

    // 设置坐标系格栅背景
    // plot->xAxis->grid()->setPen(QPen(Qt::darkGreen));
    // plot->yAxis->grid()->setPen(QPen(Qt::darkGreen));
    plot->xAxis->grid()->setSubGridVisible(true);//X栅格可见
    plot->yAxis->grid()->setSubGridVisible(true);/Y栅格可见
    //坐标内背景
    plot->axisRect()->setBackground(QBrush(QColor(30, 30, 30)));

    plot->replot();
}

//2.发送

//用信号的emit函数或者QwaitCondition的wakeOne()函数

//3.接收

//如果使用QwaitCondition,那么要单独搞个接收线程,GUI是不太可能有死循环的。

//4.绑定

//绑定函数的原型是,第1个参数是横轴对应容器,第2参数是纵轴对应容器,第三个参数可不写

void setData(const QVector<double> &keys,

                     const QVector<double> &values,

                      bool alreadySorted=false

                    );

//5.重绘

//重绘函数的原型是

void replot();

其余可参考

QCustomPlot 曲线各种属性设置介绍_customplot在曲线上添加实心圆点-CSDN博客CustomPlot 曲线各种属性设置介绍_customplot在曲线上添加实心圆点-CSDN博客QCustomPlot 曲线各种属性设置介绍_customplot在曲线上添加实心圆点-CSDN博客


网站公告

今日签到

点亮在社区的每一天
去签到