qCustomPlot简单使用

0 提前准备工作
  1. 修改窗口标题栏的图片
    在xx.pro工程文件中增加 RC_ICONS = title.ico
  2. 对高分屏的支持问题
//添加Qt的对高分屏的支持
int main(int argc, char *argv[])
{
    //添加Qt的对高分屏的支持
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
1 QCustomplot基本配置
  1. 添加qcustomplot.cpp和qcustomplot.h
  2. 这时pro文件会自动添加上qcustomplot.cpp和qcustomplot.h,这时还需要添加printsupport。由于使用到打印相关,所以需要加入printsupport,在原有的widgets 后面加入即可
QT += widgets printsupport
  1. 打开界面文件,进入图形化设计界面,向主窗口中添加一个widget区域,对着所添加的widget区域点击右键,选择“提升为”按钮,提升类名称输入“QCustomPlot”,点击添加。在之后的界面中选中QCustomPlot,点击提升按钮,我们创建的widget就被提升为QCustomPlot类了。现在我们运行一下程序,就会出现一个简单的坐标系了。
    简单的初始坐标系
  2. 在ui文件中,将添加的widget的objectName改为customPlot,进而在mainwindow的构造函数中即可添加静态曲线
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //定义两个可变数组存放绘图的坐标数据
    QVector<double> x(101),y(101);//分别存放x和y坐标的数据,101为数据长度
    //添加数据,我们这里演示y=x^3,为了正负对称,我们x从-10到+10
    for(int i=0;i<101;i++){
        x[i] = i/5 - 10;
        y[i] = x[i] * x[i] * x[i];
    }
    //向绘图区域QCustomPlot(从widget提升来的)添加一条曲线
    ui->customPlot->addGraph();
    ui->customPlot->addGraph();
    //添加数据
    ui->customPlot->graph(0)->setData(x,y);
    //设置坐标轴标签名称
    ui->customPlot->xAxis->setLabel("x");
    ui->customPlot->yAxis->setLabel("y");
    //设置坐标轴显示范围,否则我们只能看到默认的范围
    ui->customPlot->xAxis->setRange(-11,11);
    ui->customPlot->yAxis->setRange(-1100,1100);

    //重绘,这里可以不用,官方例子有,执行setData函数后自动重绘
    //我认为应该用于动态显示或者是改变坐标轴范围之后的动态显示,我们以后探索
    //ui->qCustomPlot->replot();

参考链接

在这里插入图片描述

2 动态曲线绘制
  1. 设置图层,曲线的属性
    // 添加曲线名称
    QStringList lineNames;//设置图例的文本
    lineNames << "波形1" << "波形2" << "波形3" << "波形4" << "波形5" << "波形6" << "波形7" << "波形8" << "波形9" << "波形10"\
              << "波形11" << "波形12" << "波形13" << "波形14" << "波形15" << "波形16" << "波形17" << "波形18" << "波形19" << "波形20";
    // 曲线初始颜色
    QColor initColor[20] = {QColor(0,146,152), QColor(162,0,124), QColor(241,175,0), QColor(27,79,147), QColor(229,70,70),\
                           QColor(0,140,94), QColor(178,0,31), QColor(91,189,43), QColor(0,219,219), QColor(172,172,172),\
                           QColor(0,178,191), QColor(197,124,172), QColor(243,194,70), QColor(115,136,193), QColor(245,168,154),\
                           QColor(152,208,185), QColor(223,70,41), QColor(175,215,136), QColor(157,255,255), QColor(0,0,0)};//QColor(255,255,255)};//白色
    // 图表添加20条曲线,并设置初始颜色,和图例名称
    for(int i=0; i<20; i++){
        pCurve[i] = customPlot->addGraph();
        pCurve[i]->setPen(QPen(QColor(initColor[i])));
        pCurve[i]->setName(lineNames.at(i));
    }

    // 设置背景颜色
    customPlot->setBackground(QColor(255,255,255));
    // 设置背景选择框颜色
//    ui->btnColourBack->setStyleSheet(QString("border:0px solid;background-color: %1;").arg(QColor(255,255,255).name()));

    // 曲线选择框颜色,与曲线同步颜色。这样写太复杂了,用控件指针数组在下面写过了,记得要在addGraph()之后才有效。
    //ui->btnColourCurve1->setStyleSheet("border:0px solid;background-color:rgb(0,146,152)");
    //ui->btnColourCurve1->setStyleSheet(QString("border:0px solid;background-color: %1;").arg(initColor[0].name()));
    //ui->btnColourCurve20->setStyleSheet(QString("border:0px solid;background-color: %1;").arg(pCurve[]->pen().color().name()));

    // 设置坐标轴名称
    customPlot->xAxis->setLabel("X");
    customPlot->yAxis->setLabel("Y");

    // 设置x,y坐标轴显示范围
    pointCountX = ui->txtPointCountX->text().toUInt();
    pointCountY = ui->txtPointCountY->text().toUInt();
    customPlot->xAxis->setRange(0,pointCountX);
    customPlot->yAxis->setRange(pointCountY/2*-1,pointCountY/2);

    //customPlot->axisRect()->setupFullAxesBox();//四边安装轴并显示
    //customPlot->xAxis->ticker()->setTickOrigin(1);//改变刻度原点为1
    //customPlot->xAxis->setNumberFormat("gbc");//g灵活的格式,b漂亮的指数形式,c乘号改成×
    //customPlot->xAxis->setNumberPrecision(1);//精度1
    customPlot->xAxis->ticker()->setTickCount(ui->txtMainScaleNumX->text().toUInt());//x轴主刻度个数
    customPlot->yAxis->ticker()->setTickCount(ui->txtMainScaleNumY->text().toUInt());//y轴主刻度个数
    customPlot->xAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);//可读性优于设置
    customPlot->yAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);//可读性优于设置
  1. 定时器设定、connect()函数连接信号与槽函数;
	timer = new QTimer(this);
    timer->setInterval(10);
    connect(timer,SIGNAL(timeout()),this,SLOT(TimeData_Update()));
  1. 槽函数中数据更新、曲线重新绘制;
cnt++;
    // 给曲线添加数据
    for(int i=0; i<10; i++){
//        pTxtValueCurve[i]->setText(QString::number(num-i*10,'g',8));// 显示曲线当前值
        pCurve[i]->addData(cnt, num-i*10);
    }
    for(int i=10; i<20; i++){
//        pTxtValueCurve[i]->setText(QString::number(num+(i-9)*10,'g',8));// 显示曲线当前值
        pCurve[i]->addData(cnt, num+(i-9)*10);
    }
	pPlot1->replot(QCustomPlot::rpQueuedReplot);
3 多个y轴设置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值