QCustomPlot的用法

#include <QApplication>

#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QWidget>
#include <qcustomplot.h>
#include <QMainWindow>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    
    //得到数据
    QVector<double> x(5),y0(5),y1(5),y2(5);
    for(int i=0;i<5;i++){
        x[i]=i;
        y0[i]=i+10;
        y1[i]=i+20;
        y2[i]=i+50;
    }

    //创建QCustomPlot,添加曲线graph,并设置曲线的数据
    QCustomPlot *customPlot=new QCustomPlot;
    customPlot->addGraph();
    customPlot->graph(0)->setName("fist line");
    customPlot->graph(0)->setData(x,y0);

    customPlot->addGraph();
    customPlot->graph(1)->setName("second line");
    customPlot->graph(1)->setData(x,y1);

    customPlot->addGraph();
    customPlot->graph(2)->setName("third line");
    customPlot->graph(2)->setData(x,y2);

    //自适应轴
    customPlot->graph(0)->rescaleAxes(true);
    customPlot->graph(1)->rescaleAxes(true);
    customPlot->graph(2)->rescaleAxes(true);

    //显示铭文
    customPlot->legend->setVisible(true);

    //设置customPlot可进行拽托或伸缩
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    customPlot->axisRect()->setRangeDrag(Qt::Horizontal);  //设置允许在某一方向上拽托
    customPlot->axisRect()->setRangeZoom(Qt::Horizontal);  //设置允许在某一方向上伸缩

    QMainWindow window;
    window.setCentralWidget(customPlot);
    window.resize(500,300);
    window.show();
    

/*
    //得到数据
    QVector<double> x(8),y(8);
    for(int i=0;i<8;i++){
        x[i]=i;
        y[i]=i*i;
    }

    //创建QCustomPlot,添加曲线graph,并设置曲线的数据
    QCustomPlot *customPlot=new QCustomPlot;
    customPlot->addGraph();
    customPlot->graph(0)->setName("fist line");
    customPlot->graph(0)->setData(x,y);

    //设置轴
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    textTicker->addTick(1.0, "Bacteria");
    textTicker->addTick(2.0, "Protozoa");
    textTicker->addTick(3.0, "Chromista");
    textTicker->addTick(4.0, "Plants");
    textTicker->addTick(5.0, "Fungi");
    textTicker->addTick(6.0, "Animals");
    textTicker->addTick(8.0, "Vogons");

    customPlot->xAxis->setTicker(textTicker);

    //自适应轴
    customPlot->graph(0)->rescaleAxes(true);

    //显示铭文
    customPlot->legend->setVisible(true);

    //设置customPlot可进行拽托或放大
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    QMainWindow window;
    window.setCentralWidget(customPlot);
    window.resize(500,300);
    window.show();
*/

    return app.exec();
}



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QCustomPlot 是一个强大的 C++ 库,用于创建高质量的定制图表和数据分析应用程序。如果你想要利用 QCustomPlot 在后台线程中绘制图形,以避免阻塞用户界面,可以通过以下步骤实现: 1. **开启多线程支持**:首先,确保你在项目中开启了多线程功能,这通常需要包含 `<QThread>` 和 `<QRunnable>` 头文件,并且知道如何创建并管理线程。 2. **定义绘制任务**:创建一个继承自 `QRunnable` 的类,该类将负责执行绘制操作。在该类的 `run()` 方法中,你可以调用 QCustomPlot 的绘画方法(如 `plot()` 或 `updateGraph()`)。 ```cpp class DrawTask : public QRunnable { public: DrawTask(QCustomPlot *plot, const QVector<double> &data) : plot(plot), data(data) {} void run() override { plot->beginUpdates(); plot->曲线系列()->setData(data); // 其他可能的设置,比如设置样式或调整坐标轴 plot->endUpdates(); } private: QCustomPlot *plot; QVector<double> data; }; ``` 3. **在主线程中启动绘制**:在主线程的上下文中,创建一个新的 `DrawTask` 实例并将它放入线程池或新线程中执行: ```cpp QThreadPool *threadPool = QThreadPool::globalInstance(); DrawTask *task = new DrawTask(customPlot, generateData()); threadPool->start(task); ``` 4. **处理完成通知**:如果希望在绘制完成后通知主窗口更新,可以在 `DrawTask` 类中添加信号连接,或者在 `run()` 结束后直接调用 `emit finished();` 并在主窗口的槽函数里接收这个信号。 这样,当你在后台线程绘制数据时,用户界面上的其他操作不会被阻塞,提高了程序的响应性和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值