公司要做一个采样波形的上位机,评估了QT几种绘图的图形库,最终选择QCustomplot,主要是由于精度高,刷新速率快,在这里简单介绍一下QCustomplot的使用
1.简介
QCustomplot是一个基于QT的图形库,QCustomPlot是一个小型的qt画图标类,效果可以,易用,只需要在项目中加入头文件 qcustomplot.h 和 qcustomplot.cpp 文件,然后使一个widget提升为QCustomPlot类,即可使用
2.使用
首先在官网下载 qcustomplot.h 和 qcustomplot.cpp 文件,官网中包含许多demo.
官网链接:Qt Plotting Widget QCustomPlot - Introduction
下载之后可以将使用文档qhp文件添加到QT中,可以详细查看相关类的使用说明,以及详细用法。
类似于上面截图这样,相关类和函数不明白的可以查看相关说明
3.一个demo
首先创建一个QT项目(在官方实例加了一些更改和注释)
在UI文件中添加一个widget,将qcustomplot.h和qcustomplot.cpp导入项目中,将widget提升为QCustomPlot,
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "qcustomplot.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void printToTwo(QCustomPlot *customPlot);//画两条线
bool eventFilter(QObject *obj, QEvent *event); //事件检测器
private:
Ui::MainWindow *ui;
QCustomPlot *customPlot;
QLabel *coordinateLabel;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStatusBar>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
customPlot = ui->widget;
coordinateLabel = new QLabel();
coordinateLabel->setText("点击图表查看坐标");
//coordinateLabel->move(700, 300); // 假设将标签放置在窗口的左上角
// QStatusBar *status= statusBar();
ui->statusbar->addWidget(coordinateLabel,1);
printToTwo(customPlot);
customPlot->installEventFilter(this);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == customPlot && event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
double x = customPlot->xAxis->pixelToCoord(mouseEvent->pos().x());
double y = customPlot->yAxis->pixelToCoord(mouseEvent->pos().y());
coordinateLabel->setText(QString("X: %1, Y: %2").arg(x).arg(y));
}
return QObject::eventFilter(obj, event);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::printToTwo(QCustomPlot *customPlot)
{
//每条曲线都会独占一个graph()
customPlot->addGraph();
customPlot->graph(0)->setPen(QPen(Qt::blue)); // 曲线的颜色
customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // 曲线与X轴包围区的颜色
customPlot->addGraph();//添加graph等价于添加新曲线
customPlot->graph(1)->setPen(QPen(Qt::red)); // 曲线的颜色
// 生成模拟数据点 (x-y0 第一条曲线, x-y1为第2条曲线):
QVector<double> x(251), y0(251), y1(251);
for (int i=0; i<251; ++i)
{
x[i] = i;
y0[i] = qExp(-i/150.0)*qCos(i/10.0); // 第一条曲线:y0衰减cos
y1[i] = qExp(-i/150.0); // 第二条曲线:y1衰减指数
}
// 边框右侧和上侧均显示刻度线,但不显示刻度值:
customPlot->xAxis2->setVisible(true);
customPlot->xAxis2->setTickLabels(false);
customPlot->yAxis2->setVisible(true);
customPlot->yAxis2->setTickLabels(false);
// 使上下两个X轴的范围总是相等,使左右两个Y轴的范围总是相等
connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
// 把已存在的数据填充进graph的数据区
customPlot->graph(0)->setData(x, y0);
customPlot->graph(1)->setData(x, y1);
customPlot->graph(0)->rescaleAxes();
customPlot->graph(1)->rescaleAxes(true);
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
// 立即刷新图像
ui->widget->replot();
}
运行结果:
缩放,相关选项都开启了
鼠标双击可以查看对应点坐标