Qt波形图

43 篇文章 14 订阅


头文件:

#ifndef TEST_H
#define TEST_H

#include <QtGui>
#include "ui_test.h"

class testView : public QGraphicsView
{
	Q_OBJECT

public:
	testView(QWidget* parent = NULL);
	~testView();

private slots:
	void slot_update();
};

class testScene : public QGraphicsScene
{
	Q_OBJECT

public:
	testScene();
	~testScene();

private slots:
	void slot_update();

protected:
	virtual void drawBackground(QPainter *painter, const QRectF &rect);
	virtual void drawForeground(QPainter *painter, const QRectF &rect);

private:
	QPainterPath mPath;
};

class test : public QWidget
{
	Q_OBJECT

public:
	test();
	~test();

protected:
	void paintEvent ( QPaintEvent * event );

private:
	Ui::testClass ui;
};

#endif // TEST_H

源文件:

#include "test.h"
#include <QCompleter>
#include <QDebug>
#include <QTime>
#include <QTest>

using   namespace   std;

test::test()
{
	ui.setupUi(this);

	// View && Scene
	testView* tView = new testView(this);
	ui.verticalLayout->addWidget(tView);

	testScene* scene = new testScene();

	connect(ui.btn_update, SIGNAL(clicked()), scene, SLOT(slot_update()));
	connect(ui.btn_update, SIGNAL(clicked()), tView, SLOT(slot_update()));

	tView->setScene(scene);
}

test::~test()
{

}

void test::paintEvent( QPaintEvent * event )
{
	//qDebug()<<"paintEvenet()";

	//QPainter painter(this);
	//painter.drawPath(mPath);

	QWidget::paintEvent(event);
}

testScene::testScene()
{
	setSceneRect(QRectF(0,0,2000,600));

	qsrand(QTime::currentTime().msec());

	mPath.moveTo(0,0);
	for(int i=0; i<1500; i=i+20)
	{
		int x = i;
		int y = qrand()%500;
		mPath.lineTo(QPointF(x,y));
	}
}

testScene::~testScene()
{

}

void testScene::drawBackground( QPainter *painter, const QRectF &rect )
{
	qDebug()<<"drawBackground";

	painter->save();

	painter->setBrush(Qt::darkCyan);
	painter->drawRect(rect);

	painter->setPen(Qt::yellow);
	const double w = sceneRect().width();
	const double h = sceneRect().height();
	for(int i=0; i<h; i+=100)
	{
		QLineF line(QPointF(0,i),QPointF(w,i));
		painter->drawLine(line);
	}

	painter->restore();
}

void testScene::drawForeground( QPainter *painter, const QRectF &rect )
{
	qDebug()<<"drawForeground";

	painter->save();

	painter->setPen(QPen(Qt::red,5));
	painter->drawPath(mPath);
	
	painter->restore();
}

void testScene::slot_update()
{
	qsrand(QTime::currentTime().msec());

	mPath = QPainterPath();
	mPath.moveTo(0,0);
	for(int i=0; i<1500; i=i+20)
	{
		int x = i;
		int y = qrand()%500;
		mPath.lineTo(QPointF(x,y));
	}

	double w = sceneRect().width();
	double h = sceneRect().height();
	double step = w/10;

	for(int i=0; i<w; i+=step)
	{
		update (i,0,step,h);
		//QTest::qSleep(1000);
		qDebug()<<"scene update()"<<i;
	}
}

void testView::slot_update()
{
	//double w = rect().width();
	//double h = rect().height();
	//double step = w/10;

	//for(int i=0; i<w; i+=step)
	//{
	//	update (i,0,step,h);
	//	//QTest::qSleep(1000);
	//	qDebug()<<"view update()"<<i;
	//}
}

testView::testView( QWidget* parent /*= NULL*/ )
	: QGraphicsView(parent)
{
	centerOn(0,0);

	setCacheMode(QGraphicsView::CacheBackground);
	setRenderHint(QPainter::Antialiasing, true);
	//setRenderHint(QPainter::TextAntialiasing, true);
	setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}

testView::~testView()
{

}


Qt是一个跨平台的C++应用程序开发框架,它提供了丰富和强大的图形用户界面和图形绘制功能。QCustomPlot是Qt中一个用于绘制波形图的开源库,它提供了丰富的绘图功能和配置选项。 使用QCustomPlot绘制波形图,需要引入QCustomPlot的头文件,并创建一个QCustomPlot对象。然后可以使用QCustomPlot的函数来设置图表的背景、坐标轴、数据曲线等。 绘制波形图的常用步骤如下: 1. 创建和配置QCustomPlot对象: ```cpp QCustomPlot *plot = new QCustomPlot(this); plot->setBackground(QColor(255, 255, 255)); plot->xAxis->setLabel("X轴"); plot->yAxis->setLabel("Y轴"); ``` 2. 添加坐标轴范围: ```cpp plot->xAxis->setRange(xMin, xMax); // 设置X轴范围 plot->yAxis->setRange(yMin, yMax); // 设置Y轴范围 ``` 3. 创建并配置数据曲线: ```cpp QVector<double> xData, yData; // 存储X轴和Y轴数据的向量 // 将数据添加到向量中 xData << x1 << x2 << x3 << ...; yData << y1 << y2 << y3 << ...; QCPGraph *graph = plot->addGraph(); // 创建数据曲线对象 graph->setData(xData, yData); // 设置数据曲线的X轴和Y轴数据 graph->setPen(QPen(Qt::blue)); // 设置数据曲线的画笔颜色 ``` 4. 添加图例: ```cpp plot->legend->setVisible(true); // 显示图例 plot->legend->setFont(QFont("Arial", 9)); // 设置图例字体 ``` 5. 刷新并显示绘图界面: ```cpp plot->replot(); // 刷新绘图界面 plot->show(); // 显示绘图界面 ``` 以上就是使用QCustomPlot绘制波形图的基本步骤。根据实际需求,还可以进一步配置坐标轴样式、数据点的形状和颜色、添加文字注释等。可以通过查阅QCustomPlot的文档和示例代码来了解更多绘图选项的使用方法。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值