验证中...
start.cpp
Raw Copy
1.添加库文件qcustomplot.cpp和qcustomplot.h到工程
2.pro文件添加一行QT += printsupport
3.ui设计,添加widget,并提升到QCustomPlot
draw_sine_wave.cpp
Raw Copy
//绘制动态正弦曲线
#include "dialog.h"
#include "ui_dialog.h"
#include <QTimer>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
/*************/
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(10);
ui->widget->legend->setFont(QFont("Helvetica", 12)); //设置图例字体和大小
ui->widget->legend->setVisible(true); //使能图例可见
ui->widget->setBackground(QBrush(Qt::lightGray)); //设置背景颜色
ui->widget->axisRect()->setupFullAxesBox(); //在坐标轴右侧和上方画线,和X/Y轴一起形成一个矩形
ui->widget->xAxis->setLabel("横轴"); //设置X轴文字
ui->widget->yAxis->setLabel("纵轴"); //设置Y轴文字
ui->widget->yAxis->setRangeLower(-2); //设置y轴最小值
ui->widget->yAxis->setRangeUpper(3); //设置y轴最大值
// ui->widget->yAxis->setRange(0, 3, Qt::AlignTop); //设置y轴范围-1.5~1.5
/****曲线0动态显示****/
ui->widget->addGraph();
QPen pen_line1(Qt::red, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
ui->widget->graph(0)->setPen(pen_line1);
ui->widget->graph(0)->setName("曲线0"); //设置图形0对应的图例文字
// ui->widget->graph(0)->setBrush(QBrush(QColor(255,255,0))); //设置曲线和坐标轴填充的颜色
// ui->widget->graph(0)->setData(x, y); //静态直接显示,支持QVector
}
void Dialog::update()
{
static double x;
x += 0.1;
if(x <= 50)
ui->widget->xAxis->setRangeLower(0); //前50个数据,压缩显示
else
ui->widget->xAxis->setRangeLower(x - 50); //后面的数据,平移动态显示
ui->widget->xAxis->setRangeUpper(x);
// ui->widget->xAxis->setRange(x, 20, Qt::AlignRight);
ui->widget->graph(0)->addData(x, qSin(x)); //增加数据
ui->widget->replot(); //刷新显示
}
Dialog::~Dialog()
{
delete ui;
}
QCustomPlotDemo范例.pdf

Comment list( 0 )

You need to Sign in for post a comment

Help Search

Gitee_sixth 5th_float_left_close