您的位置:首页 > 编程语言 > Qt开发

windows下Qt插件qwt中plot编程

2015-03-19 15:28 369 查看
qt 5.4.1

Qt Creator 3.3.1 (opensource)Based on Qt 5.4.1 (MSVC 2010, 32 bit)
qwt 1.2.1
实现曲线实时更新显示,数值由随机数产生

.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = qwt_test
TEMPLATE = app

SOURCES += main.cpp\
mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += E:\Qt\Qtctl\includes
#LIBS+= -lqwtd

LIBS += E:\Qt\Qtctl\build-qwt-Desktop_Qt_5_4_1_MinGW_32bit-Release\lib\libqwt.a\
E:\Qt\Qtctl\build-qwt-Desktop_Qt_5_4_1_MinGW_32bit-Release\lib\libqwtd.a\
E:\Qt\Qtctl\build-qwt-Desktop_Qt_5_4_1_MinGW_32bit-Release\lib\qwt.dll\
E:\Qt\Qtctl\build-qwt-Desktop_Qt_5_4_1_MinGW_32bit-Release\lib\qwtd.dll
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <qtimer.h>

#define QWT_DLL
#include <qwt_plot.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_renderer.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_histogram.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_magnifier.h>

#include <qwt_legend.h>
#include <qwt_legend_label.h>
#include <qwt_column_symbol.h>
#include <qwt_series_data.h>
#include <qpen.h>
#include <qwt_symbol.h>
#include <qwt_picker_machine.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private Q_SLOTS:
void timerUpDate();
void on_pushButton_clicked();

private:
QwtPlotCurve curve;
Ui::MainWindow *ui;
QwtPlotPicker *picker;
QTimer timer;
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <vector>

#include <QVector>

using namespace std;



MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{


connect(&timer,

SIGNAL(timeout()),

this,

SLOT(timerUpDate())

);

timer.setInterval(50);



ui->setupUi(this);

ui->qwtPlot->setTitle("体积变化曲线");//图表标题设置


/*---------设置画布---------*/

QwtPlotCanvas *canvas = new QwtPlotCanvas();

canvas->setPalette(Qt::white);

canvas->setBorderRadius(10);

ui->qwtPlot->setCanvas( canvas );

//ui->qwtPlot—>setAlignCanvasToScales( true );


/*-----------设置x,y坐标和范围--------------*/

ui->qwtPlot->setAxisTitle(ui->qwtPlot->yLeft,

"体积/ml" );

ui->qwtPlot->setAxisTitle(ui->qwtPlot->xBottom,

"时间/s" );

ui->qwtPlot->setAxisScale(QwtPlot::yLeft,

0.0,

10.0);

ui->qwtPlot->setAxisScale(QwtPlot::xBottom,

0.0,

30.0);


/*----------------设置栅格线-------------------*/

QwtPlotGrid *grid = new QwtPlotGrid;

grid->enableX( true );//设置网格线

grid->enableY(
152d8
true );

grid->setMajorPen( Qt::black,

0,

Qt::DotLine );

grid->attach(ui->qwtPlot);


/*----------------设置缩放-------------------*/

QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( ui->qwtPlot->canvas() );

magnifier->setMouseButton( Qt::NoButton );


//右键拖拽

(new QwtPlotPanner(ui->qwtPlot->canvas()))->setMouseButton(Qt::RightButton);


//一个选择器,十字线,以xBottom和yLeft坐标

picker = new QwtPlotPicker(QwtPlot::xBottom,

QwtPlot::yLeft,

QwtPlotPicker::CrossRubberBand,

QwtPicker::AlwaysOn,

ui->qwtPlot->canvas());


picker->setStateMachine(new QwtPickerDragPointMachine());//拖拽点起作用

picker->setRubberBandPen(QPen(QColor(Qt::black)));

picker->setTrackerPen(QColor(Qt::black));

// picker->setMousePattern(Qt::LeftButton);



/*-----------------画图曲线设置----------------------*/


//curve.setTitle( "信道"+QString( "%1 " ).arg( 1));


//这个会根据画板中的图在RightLegend显示一个图例

ui->qwtPlot->insertLegend(new QwtLegend(),

QwtPlot::RightLegend);

curve.setTitle("rand 0-9");//设置图例的标题


curve.setPen(Qt::blue,2);//设置曲线颜色 粗细

curve.setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化


QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,

//QBrush( Qt::black ), QPen( Qt::red, 0 ), QSize( 6, 6) );//设置样本点的颜色、大小

QBrush( Qt::black ), QPen( Qt::red, 0 ), QSize( 3, 3) );//设置样本点的颜色、大小

curve.setSymbol( symbol );//添加样本点形状

//delete *symbol;



/*QPolygonF points1, points2;//输入节点数据QPointF(x,y)

QVector<QPointF> *points = new QVector<QPointF>();

points->push_back(QPointF(1,0.1));

points->push_back(QPointF(2,0.2));

points->push_back(QPointF(3,0.3));

points->push_back(QPointF(4,0.1));

curve.setSamples(*points);

curve.attach( ui->qwtPlot );*/

/*

curve->setSamples(points1);

curve->attach( ui->qwtPlot );

points1<<QPointF(8,0.1)<<QPointF(9,0.1)<<QPointF(10,0.3)<<QPointF(11,0.4)

<<QPointF(12,0.5)<<QPointF(13,0.6)<<QPointF(14,0.7);

curve->setSamples(points1);

curve->attach( ui->qwtPlot );

ui->qwtPlot->setAxisScale(QwtPlot::xBottom,5.0,10.0);

*/

//delete canvas;

}


MainWindow::~MainWindow()

{

delete ui;

delete picker;

//delete curve;

timer.stop();


}

void MainWindow::timerUpDate()//定时器时间处理函数,画图

{

static QVector<QPointF> points;// = new QVector<QPointF>();


static QVector<double> data;

static float Miny= 3.0;

static float Maxy= 4.0;

//ui->qwtPlot->setAxisScale(QwtPlot::yLeft,data.size(),data.size()-10);

QwtScaleDiv QwtScaleDiv_temp;//临时变量,轴线的比例间隔类


QwtScaleDiv_temp = ui->qwtPlot->axisScaleDiv(QwtPlot::xBottom);


float  range = QwtScaleDiv_temp.range();//获取当前刻度前后间隔大小


ui->qwtPlot->setAxisScale(QwtPlot::xBottom,//X轴刻度

data.size()-range,//最小刻度设置

data.size());//最大刻度设置



float temp = qrand()%10;


if(Miny > temp)

{

Miny = temp;

}


if(Maxy < temp)

{

Maxy = temp;

}

ui->qwtPlot->setAxisScale(QwtPlot::yLeft,//y轴刻度

Miny,

Maxy);

data.push_back(temp);//数据采集


points.push_back(QPointF(data.size()-1,data[data.size()-1]));


curve.setSamples(points);

curve.attach( ui->qwtPlot );//

ui->qwtPlot->replot();//Plot重画


}


void MainWindow::on_pushButton_clicked()

{

static int flag = 1;

if( flag == 0 )

{

    timer.stop();

ui->pushButton->setText("start");

flag=1;

}

else

{

timer.start();

ui->pushButton->setText("stop");

flag = 0;

}


}


mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>

<ui version="4.0">

<class>MainWindow</class>

<widget class="QMainWindow" name="MainWindow">

<property name="geometry">

<rect>

<x>0</x>

<y>0</y>

<width>555</width>

<height>439</height>

</rect>

</property>

<property name="windowTitle">

<string>MainWindow</string>

</property>

<widget class="QWidget" name="centralWidget">

<widget class="QwtPlot" name="qwtPlot">

  <property name="geometry">

  <rect>

<x>50</x>

<y>80</y>

<width>461</width>

<height>271</height>

  </rect>

</property>

</widget>

<widget class="QPushButton" name="pushButton">

  <property name="geometry">

  <rect>

<x>50</x>

<y>20</y>

<width>75</width>

<height>23</height>

  </rect>

</property>

<property name="text">

<string>start</string>

</property>

</widget>

</widget>

<widget class="QMenuBar" name="menuBar">

 <property name="geometry">

 <rect>

 <x>0</x>

 <y>0</y>

 <width>555</width>

<height>23</height>

 </rect>

 </property>

</widget>

<widget class="QToolBar" name="mainToolBar">

<attribute name="toolBarArea">

<enum>TopToolBarArea</enum>

</attribute>

<attribute name="toolBarBreak">

<bool>false</bool>

</attribute>

</widget>

<widget class="QStatusBar" name="statusBar"/>

</widget>

<layoutdefault spacing="6" margin="11"/>

<customwidgets>

<customwidget>

<class>QwtPlot</class>

<extends>QFrame</extends>

<header>qwt_plot.h</header>

</customwidget>

</customwidgets>

<resources/>

<connections/>

</ui>


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: