16,201
社区成员
发帖
与我相关
我的任务
分享
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void timerEvent( QTimerEvent * );
public slots:
void legendChecked( const QVariant &itemInfo, bool on );
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <qwt_plot_curve.h>
#include <qwt_legend.h>
#include <qwt_plot_item.h>
#include <qwt_legend_data.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_picker.h>
#include <qwt_plot_magnifier.h>
#include <qwt_picker_machine.h>
#include <qwt_plot_zoomer.h>
QwtPlotCurve * curve = new QwtPlotCurve("89_L2-103");
//X轴
double time[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
//Y轴
double val[20] = {3, 5, 8, 7, 2, 0, 7, 9, 1,12,32,45,11,76,20,18,54,66,87,19};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 鼠标滚轮放大缩小:
// QwtPlotMagnifier *PM =
// new QwtPlotMagnifier( ui->qwtPlot->canvas() );
//鼠标左键拖动波形:
// QwtPlotPanner *PQ = new QwtPlotPanner( ui->qwtPlot->canvas() );
// 鼠标左键选择区域放大:(右键还原)
ui->qwtPlot->setTitle("实时波形");
QwtPlotZoomer* zoomer = new QwtPlotZoomer( ui->qwtPlot->canvas() );
zoomer->setRubberBandPen( QColor( Qt::black ) );
zoomer->setTrackerPen( QColor( Qt::black ) );
zoomer->setMousePattern(QwtEventPattern::MouseSelect2,Qt::RightButton, Qt::ControlModifier );
zoomer->setMousePattern(QwtEventPattern::MouseSelect3,Qt::RightButton );
ui->qwtPlot->canvas()->setPalette(QPalette (QColor(Qt::darkCyan)));
//曲线
(new QwtPlotPanner(ui->qwtPlot->canvas()))->setMouseButton(Qt::RightButton);
//y轴在放大的时候,坐标不变化
(new QwtPlotMagnifier(ui->qwtPlot->canvas()))->setAxisEnabled(QwtPlot::yLeft,false);
//一个选择器,十字线,以xBottom和yLeft坐标
QwtPlotPicker * picker;
picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,
ui->qwtPlot->canvas());
picker->setStateMachine(new QwtPickerDragPointMachine());//拖拽点起作用
picker->setRubberBandPen(QPen(QColor(Qt::white)));
picker->setTrackerPen(QColor(Qt::yellow));
curve->setPen(QPen(Qt::red, 3, Qt::SolidLine));
setAutoFillBackground(true);
//实例化
//加载数据
curve->setSamples(time,val, 20);
//加到plot,plot由IDE创建
curve->attach(ui->qwtPlot);
//启动定时器,1秒响应,用于模拟产生实时数据
this->startTimer(200);
//定时器事件
QwtLegend *legend = new QwtLegend;
//图例可以选择,Checkable
legend->setDefaultItemMode( QwtLegendData::Checkable );
//pwtPlot中插入图例
ui->qwtPlot->insertLegend(legend, QwtPlot::RightLegend );
QwtPlotGrid *grid = new QwtPlotGrid;//网格
grid->enableXMin(true);
grid->setMajorPen(QPen(Qt::red, 0, Qt::DotLine));//大格子
grid->setMinorPen(QPen(Qt::red, 0 , Qt::DotLine));//大格子里的小格子
grid->attach(ui->qwtPlot);
//连接槽,处理选择事件
connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ),this,SLOT( legendChecked( const QVariant &, bool ) ) );
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerEvent( QTimerEvent * )//定时器,
{
//所有数据前移移位,首位被覆盖
for (int i = 0; i < 19; i++)
{
val[i] = val[i+1];
}
//最后一位为新数据(这里为随机数模拟)
val[19] = qrand()%10;
//重新加载数据
curve->setSamples(time, val, 20);
//QwtPlot重绘,重要,没有这句不起作用
ui->qwtPlot->replot();
}
void MainWindow::legendChecked( const QVariant &itemInfo, bool on )
{
//获取曲线
QwtPlotItem *plotItem = ui->qwtPlot->infoToItem( itemInfo );
//根据图例选择状态,设置曲线隐藏还是显示
if ( plotItem )
plotItem->setVisible(on);
//重要,下面这句没有不起作用
ui->qwtPlot->replot();
}