公司动态
QCustomPlot使用例子
背景:处理海量波形数据如录波分析时核心痛点在于“文件读取慢”、“内存易溢出”以及“UI渲染卡顿”。环境安装与配置官网https://www.qcustomplot.com/index.php/QCustomPlot可直接从官网下载在工程中引入.h .cpp就可以此外官网也提供了几个示例程序可参考。引用这个库需要在pro文件加入QT printsupport// mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include QMainWindow #include qcustomplot.h namespace Ui { class MainWindow; } class QCustomPlot; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent nullptr); ~MainWindow(); void setupQuadraticDemo(QCustomPlot *customPlot); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H#include mainwindow.h #include ui_mainwindow.h MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui-setupUi(this); QCustomPlot* customPlot new QCustomPlot; setCentralWidget(customPlot); setupQuadraticDemo(customPlot); } MainWindow::~MainWindow() { delete ui; } void MainWindow::setupQuadraticDemo(QCustomPlot *customPlot) { QVectordouble x(101), y(101); for (int i 0; i 101; i) { x[i] i / 50.0 - 1; // -1 到 1 y[i] x[i] * x[i]; } customPlot-addGraph(); // 添加一个曲线图QGraph customPlot-graph(0)-setData(x, y); // 为曲线图添加数据 customPlot-graph(0)-setName(QString::fromLocal8Bit(customplot_quadratic_demo)); // 设置曲线图的名字 customPlot-xAxis-setLabel(x); // 设置x和y轴的标签 customPlot-yAxis-setLabel(y); customPlot-xAxis-setRange(-1, 1); // 设置x轴的范围为(-1,1) customPlot-yAxis-setRange(0, 1); customPlot-legend-setVisible(true); // 显示图例 }