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


原文地址:https://blog.csdn.net/weixin_37934835/article/details/123812102
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/837552

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.