QT之QCustomPlot的使用(二)--实时曲线

首先,我们先进行的是以时间为依据来进行动态展示,下面的代码是完整的可以进行使用;

    //动态显示的功能
    for (int i = 0; i < 10; i++)
    {
        num[i] = 0;
    }
    n = 0;
    QTimer *timer = new QTimer;
    timer->start(500);
    connect(timer, SIGNAL(timeout()), this, SLOT(Graph_Show()));
void Graph_Show()
{
    QTime t;
    t = QTime::currentTime();
    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
    n = qrand() % 50;

    for (int i = 0; i < 9; i++)
    {
        num[i] = num[i + 1];
    }

    num[9] = n;
    for (int i = 0; i < 10; i++)
    {
        temp[i] = i;
        temp1[i] = num[i];
    }

    m_pCustomPlot = new QCustomPlot(this);//初始化曲线图
    m_pCustomPlot->setGeometry(1, 1, 400, 300);//初始化曲线图坐标
    m_pCustomPlot->setLocale(QLocale(QLocale::Chinese, QLocale::China));

    m_pCustomPlot->addGraph();
    m_pCustomPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
    m_pCustomPlot->graph(0)->setPen(QPen(QColor(0, 0, 255, 200)));
    m_pCustomPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));
    m_pCustomPlot->graph(0)->setData(temp, temp1);

    m_pCustomPlot->addGraph();
    m_pCustomPlot->graph(1)->setPen(QPen(Qt::red));
    m_pCustomPlot->graph(1)->setLineStyle(QCPGraph::lsNone);
    m_pCustomPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);
    m_pCustomPlot->graph(1)->setData(temp, temp1);

    m_pCustomPlot->xAxis->setTickLabelFont(QFont(QFont().family(), 8));
    m_pCustomPlot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));

    m_pCustomPlot->xAxis->setLabel(“v”);
    m_pCustomPlot->yAxis->setLabel(“mV”);

    m_pCustomPlot->xAxis->setRange(0, 10);
    m_pCustomPlot->yAxis->setRange(-50, 50);

    m_pCustomPlot->xAxis2->setVisible(false);
    m_pCustomPlot->yAxis2->setVisible(false);
    m_pCustomPlot->xAxis2->setTicks(false);
    m_pCustomPlot->yAxis2->setTicks(false);
    m_pCustomPlot->xAxis2->setTickLabels(false);
    m_pCustomPlot->yAxis2->setTickLabels(false);

    m_pCustomPlot->replot();
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Link-Riley曲线是一种常用于滤波器设计中的图形,可以使用QCustomPlot库在QT中绘制。以下是绘制Link-Riley曲线的步骤: 1. 在QT中创建一个QWidget或QMainWindow窗口,并在该窗口中添加QCustomPlot小部件。 2. 将以下代码添加到QWidget或QMainWindow的槽函数中,以绘制Link-Riley曲线: ``` QCustomPlot *customPlot = ui->customPlot; // 获取QCustomPlot小部件的指针 customPlot->addGraph(); // 添加一个图形 customPlot->graph(0)->setPen(QPen(Qt::blue)); // 设置曲线的颜色 customPlot->graph(0)->setName("Link-Riley Curve"); // 设置曲线的名称 QVector<double> x(101), y(101); // 设置x和y向量 for (int i=0; i<101; i++) { double f = i/50.0; // 设置频率 double w = 2*M_PI*f; // 计算角频率 double a = 1/(1 + 0.414*pow(w, -1.0/2) + 0.707*pow(w, -1.0)); // 计算幅度响应 double phi = -atan(0.414*pow(w, -1.0/2) + 0.707*pow(w, -1.0)); // 计算相位响应 x[i] = f; // 设置x值 y[i] = 20*log10(a); // 计算dB值 } customPlot->graph(0)->setData(x, y); // 设置曲线的数据 customPlot->xAxis->setLabel("Frequency (Hz)"); // 设置x轴标签 customPlot->yAxis->setLabel("Magnitude (dB)"); // 设置y轴标签 customPlot->rescaleAxes(); // 自动缩放坐标轴 customPlot->replot(); // 绘制曲线 ``` 这段代码将在QCustomPlot小部件中添加一个蓝色的Link-Riley曲线。在这里,我们使用了Link-Riley滤波器的幅度响应和相位响应计算公式来计算曲线的值。在这里,我们使用了dB值来表示曲线的幅度。 3. 运行QT程序,即可看到绘制的Link-Riley曲线。可以使用QCustomPlot库的其他函数来更改曲线的样式、添加标签、设置坐标轴等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值