qwt oscilloscope实时绘制波形图

vircens 2012-11-16 10:32:13
请教各位大神,最近需要实现从串口接收数据,并用QT实时绘图的功能,已经配置好QT和QWT的环境,想用QWT的例程oscilloscope进行参照设计,但是本人是菜鸟,刚刚接触。不知道从oscilloscope例程中如何下手!望高手指点一下!
...全文
961 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
louis_liux 2014-12-06
  • 打赏
  • 举报
回复
plot当坐标轴是足够大的时候qwt坐标出现科学计数法,如何去掉科学计数法呢
xjdreamer 2014-12-03
  • 打赏
  • 举报
回复
QWT官方网站有帮助文档。http://sourceforge.jp/projects/sfnet_qwt/ 既有pdf文档,也有qch文件,可以添加到qtcreator
QQ_278397935 2014-12-02
  • 打赏
  • 举报
回复
斑竹,我是看了别人的例子改了改,主要是示例,可以交流哦。我的qq:278397935
QQ_278397935 2014-12-02
  • 打赏
  • 举报
回复
#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
QQ_278397935 2014-12-02
  • 打赏
  • 举报
回复
#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();
}
sdu_hanson 2014-06-16
  • 打赏
  • 举报
回复
自己查吧,资料有点少,
Pokeeeer 2014-06-16
  • 打赏
  • 举报
回复
我现在也正在做,你做好了吗?可以发给我看看吗?
eriklee1945 2013-06-21
  • 打赏
  • 举报
回复
其实按照oscilloscope的代码看看就明白了,不复杂的,主要看plot.h/.cpp就行 这里有个相似的例子可以看看 http://blog.sina.com.cn/s/blog_7cb9d2f90100rr9v.html
qintao212 2012-12-28
  • 打赏
  • 举报
回复
求指点。。。。
  • 打赏
  • 举报
回复
没看过QWT具体的实现。关于画图的话。。从paint()函数看应该是没有错的。
xhy2009ok 2012-12-20
  • 打赏
  • 举报
回复
怎么都没有人回,我也想问这个问题,最好有人帮忙简单将这个例子写个注释什么的

16,201

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧