QT对于统计图像、函数图像等的绘制是没有相关组件的帮助的,只有利用手工绘制图片。

QwtPlot是用来绘制二维图像的widget,继承自QFrame 和 QwtPlotDict。不过严格的说来,它只是一个视图窗口,真正的绘制设备是它的中心部件QwtPlotCanvas类。 
在它的画板上可以无限制的显示绘画组件。绘画组件可以是曲线(QwtPlotCurve)、标记(QwtPlotMarker)、网格(QwtPlotGrid)、或者其它从QwtPlotItem继承的组件。

QwtPlot拥有4个axes(轴线)

一个QwtPlot有四条坐标抽,每一个项都依附于X轴或者Y轴。每一个轴的刻度可以通过set (QwtScaleDiv)或者根据绘制的图元通过算法(QwtScaleEngine)单独配置。

变量 功能
yLeft Y axis left of the canvas.
yRight Y axis right of the canvas.
xBottom X axis below the canvas.
xTop X axis above the canvas.

常用函数接口

接口 功能
setAxisTitle 设置轴标题
enableAxis 主要是显示xTop,yRight坐标轴
setAxisMaxMajor 设置某个某个坐标轴扩大比例尺的最大间隔数目
setAxisMaxMinor 设置某个某个坐标轴缩小比例尺的最大间隔数目
setAxisScale 禁用自动缩放比例尺,为某个坐标轴指定一个修改的比例尺
insertLegend 添加图例(标注)

常用组件

组件 功能
QwtPlotCurve 曲线
QwtPlotMarker 标记
QwtPlotGrid 网格
QwtPlotHistogram 直方图
other 从QwtPlotItem继承的组件
QwtPlotItem plot 能显示的类,如果想要实现自己绘画图形,要继承此类实现rtti和draw接口
QwtPlotPanner 平移器 (用鼠标左键平移)
QwtPlotMagnifier 放大器 (用鼠标滚轮缩放)
QwtPlotCanvas 画布
QwtScaleMap 比例图—可以提供一个逻辑区域到实际区域的坐标转换
QwtScaleWidget 比例窗口
QwtScaleDiv 比例布局
QwtLegent 标注
QwtPlotLayout 布局管理器
QwtScaleDraw 自画坐标轴

QwtPlotCure简介

常见接口 功能
setPen 设置画笔
setData 设置曲线的数据
setStyle 设置曲线形式,点、直线、虚线等等
setCurveAttribute 设置曲线属性,一般设置Fitted
attch 把曲线附加到QwlPlot上

下面看一个小例子,结果如下: 

该实例绘制了两条sin曲线并自动刷新曲线显示,一个曲线是平缓曲线,另一条是硬拐点曲线,之后按动按键使两个曲线均向右平移,注意,同时移动多条曲线的情况下不要使用setData函数了,程序会错误退出,应该使用setSamples函数,具体参见代码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <Qt/qmath.h>
#include <QVector>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_magnifier.h>
#include <qwt_plot_panner.h>
#include <qwt_legend.h>
#include <qwt_point_data.h>
namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    QwtPlotCurve curve;
    QwtPlotCurve curve_r;//硬折点曲线画布
    QVector<double> xs;
    QVector<double> ys;
};

#endif // MAINWINDOW_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QtGui/QApplication>
#include <Qt/qmath.h>
#include <QVector>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_magnifier.h>
#include <qwt_plot_panner.h>
#include <qwt_legend.h>
#include <qwt_point_data.h>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->plot->resize(640,400);
    ui->plot->setAutoReplot(true);
    //设置坐标轴的名称
    ui->plot->setAxisTitle(QwtPlot::xBottom, "x->");
    ui->plot->setAxisTitle(QwtPlot::yLeft, "y->");
    //设置坐标轴的范围

    ui->plot->setAxisScale(QwtPlot::yLeft, -1.0, 1.0);
    //设置右边标注
    ui->plot->insertLegend(new QwtLegend(), QwtPlot::RightLegend);

    //使用滚轮放大/缩小
    (void) new QwtPlotMagnifier( ui->plot->canvas() );

    //使用鼠标左键平移
    (void) new QwtPlotPanner( ui->plot->canvas() );

    //计算曲线数据

    for (double x = 0; x < 2.0 * M_PI; x+=(M_PI / 10.0))
    {
        xs.append(x);
        ys.append(qSin(x));
    }
    //平滑曲线
    curve.attach(ui->plot);//把曲线附加到plot上
    curve.setSamples(xs,ys);
    curve.setStyle(QwtPlotCurve::Lines);//设置曲线上是点还是线,默认是线,所以此行可不加
    curve.setCurveAttribute(QwtPlotCurve::Fitted, true);//使曲线更光滑,不加这句曲线会很硬朗,有折点
    curve.setPen(QPen(Qt::blue));//设置画笔

    curve_r.attach(ui->plot);
    curve_r.setSamples(xs,ys);
    curve_r.setPen(QPen(Qt::green));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{

    for(int i=ys.count()-1;i>=1;i--)
    {
        ys[i]=ys.at(i-1);
    }
    ys[0]=0.5;
    curve.setSamples(xs,ys);
    curve_r.setSamples(xs,ys);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

散点图:

//头文件
class CCruvePlot:publicQwtPlot
{
public:
    CCruvePlot();
    ~CCruvePlot(void);

public:
    void drawPlotCruve();
private:
    QwtPlotCurve *  curve;
    QVector<double> xData;
    QVector<double> yData;
};

//实现文件:
#include "cruvePlot.h"
const int LineNum=7;
const int PointNum=7;
CCruvePlot::CCruvePlot(){}
CCruvePlot::~CCruvePlot(void){}

void CCruvePlot::drawPlotCruve()
{
    //QMessageBox::information(this,"Running!","Running matlab Function.....");
    setTitle("A Simple QwtPlot Demonstration");//设置标题
    insertLegend(new QwtLegend(), QwtPlot::RightLegend);//设置标线的栏
    setAxisTitle(xBottom, "x -->");
    setAxisScale(xBottom, 0.0, 10.0);
    setAxisTitle(yLeft, "y -->");
    setAxisScale(yLeft, 0, 10.0);

    QwtPlotCurve *curve = new QwtPlotCurve("lineFirst");//实例化一条曲线
    curve->attach(this);
    double *x=new double[PointNum];
    double *y=new double[PointNum];
    for(int i=0;i<PointNum;i++) {
      x[i]=i;
      y[i]=i+3;
     }

     curve->setSamples (x,y,PointNum);//传画曲线的数据
     curve->setPen(QPen(Qt::red));
     QwtPlotCurve *curve2 = new QwtPlotCurve("lineSecond");//实例化另一条线
     curve2->attach(this);
     double *x2=new double[PointNum];
     double *y2=new double[PointNum];
     for(int i=0;i<PointNum;i++){
      x2[i]=i*3;
      y2[i]=i+3;
     }

     curve2->setSamples (x2,y2,PointNum);
     curve2->setPen(QPen(Qt::blue));
    return;

}

http://blog.csdn.net/u013007900/article/details/50055353

QT学习 之 QwtPlot(数学绘图)的更多相关文章

  1. Qt Creator中的3D绘图及动画教程(参照NeHe)

    Qt Creator中的3D绘图及动画教程(参照NeHe) http://blog.csdn.net/cly116/article/details/47184729 刚刚学习了Qt Creator,发 ...

  2. Qt 学习之路 2(38):存储容器

    Qt 学习之路 2(38):存储容器 豆子 2013年1月14日 Qt 学习之路 2 38条评论 存储容器(containers)有时候也被称为集合(collections),是能够在内存中存储其它特 ...

  3. Qt 学习之路 2(30):Graphics View Framework

    Qt 学习之路 2(30):Graphics View Framework 豆子 2012年12月11日 Qt 学习之路 2 27条评论 Graphics View 提供了一种接口,用于管理大量自定义 ...

  4. Qt 学习之路 2(29):绘制设备

    Qt 学习之路 2(29):绘制设备 豆子 2012年12月3日 Qt 学习之路 2 28条评论 绘图设备是继承QPainterDevice的类.QPaintDevice就是能够进行绘制的类,也就是说 ...

  5. Qt 学习之路 2(28):坐标系统

    Qt 学习之路 2(28):坐标系统 豆子 2012年11月25日 Qt 学习之路 2 59条评论 在经历过实际操作,以及前面一节中我们见到的那个translate()函数之后,我们可以详细了解下 Q ...

  6. Qt 学习之路 2(27):渐变

    Qt 学习之路 2(27):渐变 豆子 2012年11月20日 Qt 学习之路 2 17条评论 渐变是绘图中很常见的一种功能,简单来说就是可以把几种颜色混合在一起,让它们能够自然地过渡,而不是一下子变 ...

  7. Qt 学习之路 2(26):反走样

    Qt 学习之路 2(26):反走样 豆子 2012年11月12日 Qt 学习之路 2 9条评论 我们在光栅图形显示器上绘制非水平.非垂直的直线或多边形边界时,或多或少会呈现锯齿状外观.这是因为直线和多 ...

  8. Qt 学习之路 2(25):画刷和画笔

    Home / Qt 学习之路 2 / Qt 学习之路 2(25):画刷和画笔 Qt 学习之路 2(25):画刷和画笔  豆子  2012年11月5日  Qt 学习之路 2  17条评论 前面一章我们提 ...

  9. Qt 学习之路 2(24):Qt 绘制系统简介

    Qt 学习之路 2(24):Qt 绘制系统简介 豆子 2012年10月30日 Qt 学习之路 2 77条评论 Qt 的绘图系统允许使用相同的 API 在屏幕和其它打印设备上进行绘制.整个绘图系统基于Q ...

随机推荐

  1. CSS了一个浮动导航条

    绝对浏览器窗口定位positio:FIXED: 下拉后出现返回顶部按钮 图片是我们美工给做的.55*55px,中间缝隙3px. css: html,body { height:100%}html,bo ...

  2. 最近adt升级引起的问题

    其实也不知道是什么原因引起的,因为 之前安装的adt就是23.0.3的版本,但是最近突然创建安卓工程时出现了如下问题 D:\workspace\appcompat_v7\res\values-v21\ ...

  3. 如何在内存中压缩并加密ZIP

    项目中遇到了一个问题,考虑到安全原因,需要将文件以二进制数据的方式打包成压缩文件,并且这个压缩文件是有密码的. 去Google上找了些API,下载来看了下,琢磨出了以下方法 首先放API: <! ...

  4. 用js捕捉鼠标连续点击三次事件怎么实现啊

    var count = 0, timer; document.onclick = function(){ if(count < 2){ if(timer){ clearTimeout(timer ...

  5. YII框架下实现密码修改

    YII2 实现修改密码功能 主要难点: 1.密码加密 YII2对密码加密生成的结果是不同的,即用相同的初始密码在不同时间得到的加密结果不同,所以我们不能用常用的方法去验证密码是否正确(将密码加密后与数 ...

  6. cocos2dx vs2010打开打印窗口

    vs2010下调试时,有时需要有打印窗口. 在main.cpp函数的开始处,加入一下 AllocConsole(); freopen("CONIN$", "r" ...

  7. Spring Boot简介

    Spring Boot简介 Spring Boot是为了简化Spring开发而生,从Spring 3.x开始,Spring社区的发展方向就是弱化xml配置文件而加大注解的戏份.最近召开的SpringO ...

  8. Java Date 和 Calendar

    Java 语言的Date(日期),Calendar(日历),DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分.日期是商业逻辑计算一个关键的部分,所有的开发者都应该能够计算 ...

  9. Memcached源代码分析 - Memcached源代码分析之消息回应(3)

    文章列表: <Memcached源代码分析 - Memcached源代码分析之基于Libevent的网络模型(1)> <Memcached源代码分析 - Memcached源代码分析 ...

  10. Foundation Sorting: Shellsort

    /* Shell Sorting. * Implemention history:. * 2013-09-15, Mars Fu, first version. */ /* [Shell Sortin ...