Qt波形图

Qt波形图_第1张图片

头文件:

#ifndef TEST_H
#define TEST_H

#include 
#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 
#include 
#include 
#include 

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; idrawLine(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


你可能感兴趣的:(Qt,初识Qt)