《Qt开发》基于QWT的曲线图绘制

这篇具有很好参考价值的文章主要介绍了《Qt开发》基于QWT的曲线图绘制。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Qwt绘制曲线图

该示例包含以下功能:

1.使用qwt绘制曲线图

2.通过鼠标实现绘图的缩放,只缩放x轴或只缩放y轴或同时缩放

3.设置绘图区域和绘图区域外的背景颜色

4.通过点击图例实现曲线的显示和隐藏

QwtPlot绘图部件

头文件

#include <qwt_plot.h>

枚举类型

enum Axis {yLeft, yRight, xBottom, xTop,axisCnt}

坐标轴

enum LegendPosition { LeftLegend, RightLegend, BottomLegend, TopLegend }

图例位置

常用函数

QwtPlot (QWidget =NULL)

QwtPlot *plot=newQwtPlot()

QwtPlot (const QwtText &title, QWidget =NULL)

QwtPlot *plot=new QwtPlot(QwtText("plot demo"));

void setAutoReplot (bool=true)

设置自动绘图

void setTitle (const QString &)

plot->setTitle(“plot demo”);

void setTitle (const QwtText &t)

plot->setTitle(QwtText(“plot demo”));

void setCanvasBackground (const QBrush &)

plot->setCanvasBackground(Qt::white);

void setAxisAutoScale (int axisId, bool on=true)

plot->setAxisAutoScale(QwtPlot::xBottom, true);

void enableAxis (int axisId, bool tf=true)

plot->setAxisAutoScale(QwtPlot::xBottom, false);

void setAxisFont (int axisId, const QFont &f)

plot->setAxisFont(QwtPlot::xBottom,QFont("宋体"));

void setAxisScale (int axisId, double min, double max, double step=0)

plot->setAxisScale(QwtPlot::xBottom,0,50,5);

void setAxisLabelAlignment (int axisId, Qt::Alignment)

plot->setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignBaseline | Qt::AlignVCenter);

void setAxisLabelRotation (int axisId, double rotation)

plot->setAxisLabelRotation(QwtPlot::xBottom,30);

void setAxisTitle (int axisId, const QString &)

plot->setAxisTitle(QwtPlot::yLeft,"yLabel");

void setAxisTitle (int axisId, const QwtText &)

plot->setAxisTitle(QwtPlot::yLeft,QwtText("yLabel"));

void setAxisMaxMinor (int axisId, int maxMinor)

plot->setAxisMaxMinor(QwtPlot::xBottom, 5);

设置每个大格刻度分为几个小格刻度

void setAxisMaxMajor (int axisId, int maxMajor)

plot->setAxisMaxMajor(QwtPlot::xBottom, 5);

设置xBottom轴有多少个大格

void insertLegend (QwtAbstractLegend , LegendPosition=QwtPlot::RightLegend, double ratio=-1.0)

QwtLegend *legend = new QwtLegend;

legend->setDefaultItemMode(QwtLegendData::Checkable);

plot->insertLegend(legend,QwtPlot::RightLegend);

QwtPlotCurve绘图曲线

头文件

#include <qwt_plot_curve.h>

枚举类型

enum CurveStyle {NoCurve = -1, Lines, Sticks, Steps,Dots, UserCurve = 100 }

enum CurveAttribute { Inverted = 0x01, Fitted = 0x02 }

enum LegendAttribute { LegendNoAttribute = 0x00, LegendShowLine = 0x01, LegendShowSymbol = 0x02,
LegendShowBrush = 0x04 }

enum PaintAttribute { ClipPolygons = 0x01, FilterPoints = 0x02, MinimizeMemory = 0x04, ImageBuffer = 0x08}

常用函数

QwtPlotCurve (const QString &title=QString::null)
QwtPlotCurve (const QwtText &title)

void setRawSamples (const double xData, const double yData, int size)

void setSamples (const double xData, const double yData, int size)

void setSamples (const QVector< double > &xData, const QVector< double > &yData)

void setSamples (const QVector< QPointF > &)

void setSamples (QwtSeriesData< QPointF > )

double minXValue () const

double maxXValue () const

double minYValue () const

double maxYValue () const

void setPen (const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)

void setPen (const QPen &)

void setBrush (const QBrush &)

void setSymbol (QwtSymbol )

void setStyle (CurveStyle style)

QwtPlotGrid绘图网格

头文件

#include <qwt_plot_grid.h>

常用函数

void enableX (bool tf)

void enableY (bool tf)

void enableXMin (bool tf)

void enableYMin (bool tf)

void setPen (const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)

void setPen (const QPen &)

void setMajorPen (const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)

void setMajorPen (const QPen &)

void setMinorPen (const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)

void setMinorPen (const QPen &p)

QwtSymbol绘图标识符

头文件

#include <qwt_symbol.h>

枚举类型

enum Style {NoSymbol = -1, Ellipse, Rect, Diamond,Triangle, DTriangle, UTriangle, LTriangle,RTriangle, Cross, XCross, HLine,VLine, Star1, Star2, Hexagon,Path, Pixmap, Graphic, SvgDocument,UserStyle = 1000 }

enum CachePolicy { NoCache, Cache, AutoCache }

常用函数

QwtSymbol (Style=NoSymbol)

QwtSymbol (Style, const QBrush &, const QPen &, const QSize &)

QwtSymbol (const QPainterPath &, const QBrush &, const QPen &)

void setSize (const QSize &)

void setSize (int width, int height=-1)

virtual void setColor (const QColor &)

void setBrush (const QBrush &b)

void setPen (const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)

void setPen (const QPen &)

void setStyle (Style)

QwtPlotPicker绘图拾取器,获取坐标信息

QwtPlotPicker能够获取Plot中的以原点为起点的坐标并通过跟随鼠标的Label显示,

头文件

#include <qwt_plot_picker.h>

常用函数

QwtPlotPicker (int xAxis, int yAxis, QWidget )

QwtPlotPicker (int xAxis, int yAxis, RubberBand rubberBand, DisplayMode trackerMode, QWidget )

QwtPicker

头文件

#include <qwt_picker.h>

枚举类型

enum RubberBand {
NoRubberBand = 0, HLineRubberBand, VLineRubberBand, CrossRubberBand,
RectRubberBand, EllipseRubberBand, PolygonRubberBand, UserRubberBand = 100 }

enum DisplayMode { AlwaysOff, AlwaysOn, ActiveOnly }

enum ResizeMode { Stretch, KeepSize }

常用函数

QwtPicker (QWidget parent)

QwtPicker (RubberBand rubberBand, DisplayMode trackerMode, QWidget )

void setStateMachine (QwtPickerMachine )

void setRubberBand (RubberBand)

void setTrackerMode (DisplayMode)

void setRubberBandPen (const QPen &)

void setTrackerPen (const QPen &)

void setTrackerFont (const QFont &)

更多详情查看用户手册。

示例程序

头文件内容如下:

#ifndef QWTLINEEG_H

#define QWTLINEEG_H

#include <QtWidgets/QWidget>

#include "ui_qwtlineeg.h"

#include "QWT\qwt_plot.h"

#include "QWT\qwt_plot_grid.h"

#include "QWT\qwt_plot_curve.h"

#include "QWT\qwt_plot_picker.h"

#include "QWT\qwt_picker_machine.h"

#include "qmath.h"

#include "QWT\qwt_symbol.h"

#include "QWT\qwt_plot_magnifier.h"

#include "QWT\qwt_plot_panner.h"

#include "QWT\qwt_legend.h"

#include "QWT\qwt_plot_zoomer.h"

#include "QWT\qwt_text.h"

class QwtLineEg : public QWidget

{

    Q_OBJECT

public:

    QwtLineEg(QWidget *parent = 0);

    ~QwtLineEg();

private:

    Ui::QwtLineEgClass ui;

   

    QwtPlot *plot;

    void DrawLine();  //绘制曲线

    void ZoomInOut(); //缩放

    public slots:

    void showItem(const QVariant &itemInfo, bool on);

};

#endif // QWTLINEEG_H

源文件内容如下:

#include "qwtlineeg.h"

QwtLineEg::QwtLineEg(QWidget *parent)

    : QWidget(parent)

{

    ui.setupUi(this);

    DrawLine();

    ZoomInOut();

}

QwtLineEg::~QwtLineEg()

{

}

void QwtLineEg::DrawLine()

{

    //设置绘图对象

    plot = new QwtPlot(QwtText("plot demo"));

    //plot->setTitle("plot demo");

    plot->setCanvasBackground(QColor(255,255,255));  //设置绘图区域的颜色

    plot->setAutoReplot(true);

    QPalette pal = palette();

    pal.setBrush(QPalette::Window, QColor(255, 231, 147));  //设置整个界面的颜色

    setPalette(pal);

   

    //设置坐标轴

    plot->setAxisTitle(QwtPlot::yLeft,"yLabel");

    plot->setAxisTitle(QwtPlot::xBottom,"xBottom");

    plot->setAxisFont(QwtPlot::xBottom,QFont("宋体"));

    plot->setAxisScale(QwtPlot::xBottom,0,50,5);

   

    plot->setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignBaseline | Qt::AlignVCenter);

    plot->setAxisLabelRotation(QwtPlot::xBottom,30);

    //设置绘图区域网格

    QwtPlotGrid *grid = new QwtPlotGrid();

    grid->setMajorPen(Qt::darkGreen, 0, Qt::DotLine);

    grid->attach(plot);

    //设置图例

    QwtLegend *legend = new QwtLegend;

    legend->setDefaultItemMode(QwtLegendData::Checkable);

    plot->insertLegend(legend,QwtPlot::RightLegend);

    connect(legend, SIGNAL(checked(const QVariant &,bool,int)), SLOT(showItem(const QVariant &,bool)));

    //设置曲线对象

    QwtPlotCurve *curve = new QwtPlotCurve();

    curve->setTitle("--sin--");

    curve->setPen(Qt::green, 2);

    curve->setRenderHint(QwtPlotItem::RenderAntialiased, true);

   

    QwtPlotCurve *curve2 = new QwtPlotCurve();

    curve2->setTitle("--cos--");

    curve2->setPen(Qt::blue, 2);

    curve2->setRenderHint(QwtPlotItem::RenderAntialiased, true);

   

    //准备符号对象

    QwtSymbol *symbol = new QwtSymbol(QwtSymbol::Star2);

    symbol->setPen(Qt::red);

    symbol->setSize(7);

    curve->setSymbol(symbol);

    QwtSymbol *symbol2 = new QwtSymbol(QwtSymbol::XCross);

    symbol2->setPen(Qt::cyan);

    symbol2->setSize(7);

    curve2->setSymbol(symbol2);

    //生成数据点并添加到曲线

    QPolygonF points,points2;

    for (int i = 0; i < 50; i++)

    {

        double y = sin(i * 2 * M_PI / 50);

        points.append(QPointF(i,y));

        y = cos(i * 2 * M_PI / 50);

        points2.append(QPointF(i, y));

    }

    curve->setSamples(points);

    curve->attach(plot);  //将曲线添加到绘图对象

    curve2->setSamples(points2);

    curve2->attach(plot);  //将曲线添加到绘图对象

    //显示鼠标位置

    QwtPlotPicker *picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,

        QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn, plot->canvas());

    picker->setStateMachine(new QwtPickerDragPointMachine());

    picker->setRubberBandPen(QColor(Qt::green));

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

    //设置绘图区域布局

    QHBoxLayout *hLayout = new QHBoxLayout(ui.widget);

    hLayout->setContentsMargins(QMargins(0, 0, 0, 0));

    hLayout->setMargin(0);

    hLayout->addWidget(plot);

}

void QwtLineEg::ZoomInOut()

{

    //鼠标控制平移和缩放

    QwtPlotMagnifier *magnifier = new QwtPlotMagnifier(plot->canvas()); //使用滚轮缩放

    QwtPanner *panner = new QwtPlotPanner(plot->canvas());  //使用鼠标左键平移

    //Shift+滚轮,X轴缩放

    QwtPlotMagnifier *zoomX = new QwtPlotMagnifier(plot->canvas());

    QwtPlotMagnifier *zoomY = new QwtPlotMagnifier(plot->canvas());

    zoomX->setWheelModifiers(Qt::ShiftModifier);

    zoomX->setAxisEnabled(QwtPlot::xBottom, true);

    zoomX->setAxisEnabled(QwtPlot::yLeft, false);

    //Ctrl+滚轮,Y轴缩放

    zoomY->setWheelModifiers(Qt::ControlModifier);

    zoomY->setAxisEnabled(QwtPlot::xBottom, false);

    zoomY->setAxisEnabled(QwtPlot::yLeft, true);

}

void QwtLineEg::showItem(const QVariant &itemInfo, bool on)

{

    //获取曲线

    QwtPlotItem *plotItem = plot->infoToItem(itemInfo);

    //根据曲线选择状态,设置曲线隐藏和显示,选中隐藏

    if (plotItem)

        plotItem->setVisible(!on);

    plot->replot();

}

运行结果如下,鼠标点击图例可以实现曲线显示和隐藏的切换

《Qt开发》基于QWT的曲线图绘制

 文章来源地址https://www.toymoban.com/news/detail-404408.html

到了这里,关于《Qt开发》基于QWT的曲线图绘制的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • PyLab绘制曲线图

    PyLab绘制曲线图

    PyLab 是一个面向 Matplotlib 的绘图库接口,其语法和 MATLAB 十分相近。它和 Pyplot 模快都够实现 Matplotlib 的绘图功能。PyLab 是一个单独的模块,随 Matplotlib 软件包一起安装,该模块的导包方式和 Pyplot 不同,如下所示: PyLab 是一个很便捷的模块,下面对它的使用方法做相应的介绍

    2024年02月16日
    浏览(13)
  • 【QCustomPlot】绘制 x-y 曲线图

    【QCustomPlot】绘制 x-y 曲线图

    使用 QCustomPlot 绘图库辅助开发时整理的学习笔记。同系列文章目录可见 《绘图库 QCustomPlot 学习笔记》目录。本篇介绍如何使用 QCustomPlot 绘制 x-y 曲线图,需要 x 轴数据与 y 轴数据都已知,示例中使用的 QCustomPlot 版本为 Version 2.1.1 ,QT 版本为 5.9.2 。 目录 说明 1. 示例工程配

    2024年02月09日
    浏览(12)
  • 基于IMX6ULL的AP3216C的QT动态数据曲线图显示

    基于IMX6ULL的AP3216C的QT动态数据曲线图显示

    前言: 本文为手把手教学 Linux+QT 的典型基础项目 AP3216C 的数据折线图显示,项目使用正点原子的 IMX6ULL  阿尔法( Cortex-A7 系列)开发板。项目需要实现 AP3216C 在 Linux 系统下的驱动,使用 QT 设计 AP3216C 的数据显示页面作为项目的应用层。该项目属于非常简单的入门级项目,核心

    2024年02月16日
    浏览(18)
  • QT图表-折线图、曲线图

    QT图表-折线图、曲线图

    时间记录:2024/1/15 1.添加图表模块 .pro项目管理文件中添加charts模块 QChart类:图表类 QChartView类:图表显示类 2.ui文件中添加QChartView组件 (1)选择一个QGrapicsView组件将其拖拽到ui界面上合适位置 (2)右键新添加的QGrapicsView组件,将组件提升为QChartView组件 3.添加QChartView类的命

    2024年01月18日
    浏览(11)
  • Qt+C++串口调试接收发送数据曲线图

    Qt+C++串口调试接收发送数据曲线图

    程序示例精选 Qt+C++串口调试接收发送数据曲线图 如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助! 这篇博客针对Qt+C++串口调试接收发送数据曲线图编写代码,代码整洁,规则,易读。 学习与应用推荐首选。 一、所需工具软件 二、使用步骤

    2024年02月11日
    浏览(46)
  • 微信小程序Canvas绘制曲线图饼图柱状图雷达图蛛网图实现(附源码)
  • YOLOv5|YOLOv7|YOLOv8改进之实验结果(四):将多种算法的Loss精度曲线图绘制到一张图上,便于YOLOv5、v7系列模型对比实验获取更多精度数据,丰富实验数据

    YOLOv5|YOLOv7|YOLOv8改进之实验结果(四):将多种算法的Loss精度曲线图绘制到一张图上,便于YOLOv5、v7系列模型对比实验获取更多精度数据,丰富实验数据

    💡该教程为改进YOLO高阶指南,属于 《芒果书》 📚系列,包含大量的原创首发改进方式🚀 💡更多改进内容📚可以点击查看:YOLOv5改进、YOLOv7改进、YOLOv8改进、YOLOX改进原创目录 | 老师联袂推荐🏆 💡 🚀🚀🚀本博客内含·改进源代码·,按步骤操作运行改进后的代码即可

    2023年04月17日
    浏览(49)
  • 【KV260】利用XADC生成芯片温度曲线图

    【KV260】利用XADC生成芯片温度曲线图

    如何在没有温度计的情况下,监控芯片的温度呢? Xilinx不仅提供了内置的XADC来观察温度,而且还可以生成如下的曲线图 具体操作如下 这时可以看到当前温度,最小温度,最大温度 上面是直接读取温度值。如果我们要长时间观察温度变化情况怎么办呢? 如下图 在黑色曲线区

    2024年02月15日
    浏览(10)
  • echarts折线图流动特效的实现(非平滑曲线)

    echarts折线图流动特效的实现(非平滑曲线)

    echarts官网:series-lines 注意:流动特效只支持非平滑曲线(smooth:false) series-lines路径图 : 用于带有起点和终点信息的线数据的绘制,主要用于地图上的航线,路线的可视化。 ECharts 2.x 里会用地图上的 markLine 去绘制迁徙效果,在 ECharts 3 里建议使用单独的 lines 类型图表。

    2024年02月14日
    浏览(12)
  • 基于Qt4开发曲线绘制交互软件Plotter

    基于Qt4开发曲线绘制交互软件Plotter

    目前市面上有很多曲线绘制软件,但其交互功能较差。比如,想要实现数据的交互,同步联动等,都需要大量繁琐的人工操作。所以讲想开发一款轻量级的曲线绘制交互软件。下面就以此为案例,记录一下基于Qt4的开发过程。 目录 1 需求 2 技术路线 3 开发流程 1 框架搭建 2

    2024年01月21日
    浏览(9)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包