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();
}

猜你喜欢

转载自blog.csdn.net/u012288722/article/details/79137000