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

2024-03-23 07:20
文章标签 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的曲线图绘制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/837552

相关文章

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

Qt 中 isHidden 和 isVisible 的区别与使用小结

《Qt中isHidden和isVisible的区别与使用小结》Qt中的isHidden()和isVisible()方法都用于查询组件显示或隐藏状态,然而,它们有很大的区别,了解它们对于正确操... 目录1. 基础概念2. 区别清见3. 实际案例4. 注意事项5. 总结1. 基础概念Qt 中的 isHidd

QT移植到RK3568开发板的方法步骤

《QT移植到RK3568开发板的方法步骤》本文主要介绍了QT移植到RK3568开发板的方法步骤,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录前言一、获取SDK1. 安装依赖2. 获取SDK资源包3. SDK工程目录介绍4. 获取补丁包二

Qt把文件夹从A移动到B的实现示例

《Qt把文件夹从A移动到B的实现示例》本文主要介绍了Qt把文件夹从A移动到B的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录如何移动一个文件? 如何移动文件夹(包含里面的全部内容):如何删除文件夹:QT 文件复制,移动(

基于Python开发PDF转PNG的可视化工具

《基于Python开发PDF转PNG的可视化工具》在数字文档处理领域,PDF到图像格式的转换是常见需求,本文介绍如何利用Python的PyMuPDF库和Tkinter框架开发一个带图形界面的PDF转P... 目录一、引言二、功能特性三、技术架构1. 技术栈组成2. 系统架构javascript设计3.效果图

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T