Qt 使用 qcustomplot 绘制曲线例子
发布网友
发布时间:2022-12-17 16:41
我来回答
共1个回答
热心网友
时间:2023-10-03 05:48
源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cwidgetcheckbox.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_pPlot = new QCustomPlot;
m_pPlot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom|QCP::iMultiSelect|QCP::iSelectOther|QCP::iSelectItems);
m_pPlot->axisRect()->setupFullAxesBox();
m_pPlot->xAxis->setLabel("时间");
m_pPlot->yAxis->setLabel("值");
m_pPlot->xAxis->setRange(0,30);
m_pPlot->yAxis->setRange(0,100);
m_pPlot->legend->setVisible(true);
//m_pPlot->graph( index )->setName(QString::fromStdString(nameVec[0][0]));
ui->verticalLayout->addWidget( m_pPlot );
m_id = startTimer(10);
m_line = m_pPlot->addGraph();
m_line->setName( tr("test") );
m_line->setPen(QPen(QColor(255,0,0)));
m_line->setScatterStyle(QCPScatterStyle::ssDisc);
m_index = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
double MainWindow::getData(double time)
{
double s = qCos( time * M_PI * 2 ) ;
return s;
}
void MainWindow::timerEvent(QTimerEvent *event)
{
int data = qrand()%20;
m_line->addData(m_index, data);
//m_line->addData(vecX,m_vetData);
m_pPlot->replot();
m_index++;
if( m_index > 31 )
{
m_pPlot->xAxis->setRange(m_index-30,m_index);
}
QMainWindow::timerEvent(event);
}
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "qcustomplot.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
double getData(double time);
protected:
void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;
//void timerEvent(QTimerEvent *event);
private slots:
private:
Ui::MainWindow *ui;
QCPGraph * m_line;
QCustomPlot * m_pPlot;
QVector m_vetData;
int m_index;
int m_id;
};
#endif // MAINWINDOW_H
热心网友
时间:2023-10-03 05:48
源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cwidgetcheckbox.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_pPlot = new QCustomPlot;
m_pPlot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom|QCP::iMultiSelect|QCP::iSelectOther|QCP::iSelectItems);
m_pPlot->axisRect()->setupFullAxesBox();
m_pPlot->xAxis->setLabel("时间");
m_pPlot->yAxis->setLabel("值");
m_pPlot->xAxis->setRange(0,30);
m_pPlot->yAxis->setRange(0,100);
m_pPlot->legend->setVisible(true);
//m_pPlot->graph( index )->setName(QString::fromStdString(nameVec[0][0]));
ui->verticalLayout->addWidget( m_pPlot );
m_id = startTimer(10);
m_line = m_pPlot->addGraph();
m_line->setName( tr("test") );
m_line->setPen(QPen(QColor(255,0,0)));
m_line->setScatterStyle(QCPScatterStyle::ssDisc);
m_index = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
double MainWindow::getData(double time)
{
double s = qCos( time * M_PI * 2 ) ;
return s;
}
void MainWindow::timerEvent(QTimerEvent *event)
{
int data = qrand()%20;
m_line->addData(m_index, data);
//m_line->addData(vecX,m_vetData);
m_pPlot->replot();
m_index++;
if( m_index > 31 )
{
m_pPlot->xAxis->setRange(m_index-30,m_index);
}
QMainWindow::timerEvent(event);
}
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "qcustomplot.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
double getData(double time);
protected:
void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;
//void timerEvent(QTimerEvent *event);
private slots:
private:
Ui::MainWindow *ui;
QCPGraph * m_line;
QCustomPlot * m_pPlot;
QVector m_vetData;
int m_index;
int m_id;
};
#endif // MAINWINDOW_H