关于Qwt的使用-QwtPlot
转自:http://blog.csdn.net/zhuyunfei/article/details/51150098
1.新建一个qt新工程,往主界面中拖动一个QwtPlot控件,保存后关闭。
ps:如果在设计界面没有,试试qt界面用qt designer打开试试
2.下面开始QwtPlot的简单使用
a)控件的位置移动和大小设置
int width = this->width()-10;int height = this->height() - 50;//qDebug()<<width<<height;//移动QwtPlot控件到父窗口的0,0起始位ui->qwtPlot->move(0,0);//设置QwtPlot控件的大小ui->qwtPlot->resize(width,height);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
可以自己调整下move参数看看效果。
b)设置标题有两种形式
1)参数为QString
//参数是QStringui->qwtPlot->setTitle(QObject::trUtf8("QwtPlot Test"));
- 1
- 2
效果如下:
2)参数为QwtText,此种类型为我们提供了包括字体,颜色等设置
QwtText t;//设置标题名t.setText(QObject::trUtf8("QwtPlot Test"));//设置字体QFont font;//设置粗体font.setBold(true);//设置斜体font.setItalic(true);t.setFont(font);//设置颜色t.setColor(QColor(255,0,0));//设置标题背景色QBrush brush(QColor(0,0,255));t.setBackgroundBrush(brush);ui->qwtPlot->setTitle(t);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
运行效果如下
c)坐标轴控制
QwtPlot为我们提供了4条坐标轴,分别是底部坐标轴,左坐标轴,顶部坐标轴,右坐标轴
左坐标轴 QwtPlot::yLeft
右坐标轴 QwtPlot::yRight
底部坐标轴 QwtPlot::xBottom
顶部坐标轴 QwtPlot::xTop
我们可以先来看看代码实现
//setAxisScale四个参数的含义分别是//第一个参数表示坐标轴,//第二个参数表示坐标轴最小值//第三个参数表示坐标轴最大值//第四个参数表示步进ui->qwtPlot->setAxisScale(QwtPlot::yLeft,0,100,10);ui->qwtPlot->setAxisScale(QwtPlot::xBottom,0,100,10);ui->qwtPlot->setAxisScale(QwtPlot::yRight,0,100,10);ui->qwtPlot->setAxisScale(QwtPlot::xTop,0,100,10);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
运行下看看效果:
奇怪,为什么我们设置了右坐标轴和顶部坐标轴,为什么不显示呢?
我们在代码中加入如下语句看看输出结果
//axisEnable用于判断坐标轴是否开启了使用功能qDebug()<<ui->qwtPlot->axisEnabled(QwtPlot::yLeft);qDebug()<<ui->qwtPlot->axisEnabled(QwtPlot::xBottom);qDebug()<<ui->qwtPlot->axisEnabled(QwtPlot::yRight);qDebug()<<ui->qwtPlot->axisEnabled(QwtPlot::xTop);
- 1
- 2
- 3
- 4
- 5
控制台输出结果如下:
从结果我们可知,原来右坐标轴和顶部坐标轴处于不可用状态,所以没有显示。
那么就很容易了,只要让顶部坐标轴和右坐标轴enable即可,我们在qDebug之前加入如下两句
ui->qwtPlot->enableAxis(QwtPlot::yRight,true);ui->qwtPlot->enableAxis(QwtPlot::xTop,true);
- 1
- 2
再来看看运行效果和控制台输出结果:
可见,四条坐标轴都显示出来了,QwtPlot默认enable的坐标轴是左坐标轴和底部坐标轴。
我们可以给每条坐标轴设置一个title,赋予具体含义:
ui->qwtPlot->setAxisTitle(QwtPlot::yLeft,QObject::trUtf8("Left"));ui->qwtPlot->setAxisTitle(QwtPlot::yRight,QObject::trUtf8("Right"));ui->qwtPlot->setAxisTitle(QwtPlot::xBottom,QObject::trUtf8("Bottom"));ui->qwtPlot->setAxisTitle(QwtPlot::xTop,QObject::trUtf8("Top"));
- 1
- 2
- 3
- 4
运行效果如下:
这里的setAxisTitle设置坐标轴标题函数的第二个参数也可以是QwtText类型。
我们也可以更改坐标轴坐标值的字体
//微软雅黑//黑体QFont f("宋体", 12);//Helvetica [Cronyx]ui->qwtPlot->setAxisFont(QwtPlot::xBottom,f);
d)画图:终于到往QwtPlot控件内画曲线了。
步骤:
1.创建plot组件
2.往plot组件加入数据
3.把plot组件附加到qwtPlot中
//数据x,y值保存QVector<QPointF> vector;for(int i =0;i<100;i++){QPointF point;point.setX(i);int y = 20*sin(i*M_PI/10) + 50;point.setY(y);vector.append(point);}//构造曲线数据QwtPointSeriesData* series = new QwtPointSeriesData(vector);//create plot itemQwtPlotCurve* curve1 = new QwtPlotCurve("Curve 1");//设置数据curve1->setData(series);//把曲线附加到qwtPlot上curve1->attach(ui->qwtPlot);ui->qwtPlot->replot();ui->qwtPlot->show();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
其中的QwtPlotCurve即为plot组件,由此可知我们可以往qwtPlot中加入多个组件。
其中QwtPlotCurve的组件有:
QwtPlotCurve | 曲线 |
QwtPlotMarker | 标记 |
QwtPlotGrid | 网格 |
QwtPlotHistogram | 直方图 |
other | 从QwtPlotItem继承的组件 |
e)我们下面来试试两个曲线组件
把之前的拷贝复制修改下:
//数据x,y值保存QVector<QPointF> vector2;for(int i =0;i<100;i++){QPointF point;point.setX(i);int y = 10*sin(i*M_PI/10) + 20;point.setY(y);vector2.append(point);}//构造曲线数据QwtPointSeriesData* series2 = new QwtPointSeriesData(vector2);//create plot itemQwtPlotCurve* curve2 = new QwtPlotCurve("Curve 2");//设置数据curve2->setData(series2);//把曲线附加到qwtPlot上curve2->attach(ui->qwtPlot);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
这时我们看看结果:
f)然后我们试试给曲线设置画笔,在将curve附加到qwtPlot之前分别为curve1和curve2加入如下两句:
//设置画笔curve1->setPen(QColor(255,0,0),2,Qt::SolidLine);......//设置画笔curve2->setPen(QColor(0,0,255),2,Qt::DotLine);
- 1
- 2
- 3
- 4
- 5
- 6
再来看看效果图:
g)设置填充画刷,还是在附加到qwtPlot之前加入以下语句
//设置填充画刷QBrush brush2(QColor(128,128,128));curve1->setBrush(brush2);......//设置填充画刷QBrush brush3(QColor(192,192,192));curve2->setBrush(brush3);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
看看效果图
h)加入网格,在代码最后添加如下:
//加入网格QwtPlotGrid* grid = new QwtPlotGrid();grid->setPen(QColor(222,222,222),1);grid->attach(ui->qwtPlot);
- 1
- 2
- 3
- 4
效果如图:
i)最后介绍下如何删除所画的图形,很简单
//删除所画的图形curve1->detach();curve2->detach();
- 1
- 2
- 3
也就是把相关组件取消关联即可。
好了关于QwtPlot的简单使用就介绍到这了,关于QwtPlot的更多功能请参看文档实现。
本实例的所有源码附加如下:
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <qwt_plot.h>
int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
mainwindow.h
#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();
private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp,注意添加的几个头文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtMath>
#include <qwt_plot.h>
#include <qwt_plot_curve.h> //是包含QwtPointSeriesData类的头文件
#include <qwt_plot_grid.h>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);ui->qwtPlot->setTitle(QObject::trUtf8("层轮廓信息"));//设置标题ui->qwtPlot->setAxisScale(QwtPlot::yLeft,0,100,10);ui->qwtPlot->setAxisScale(QwtPlot::xBottom,0,100,20);ui->qwtPlot->setAxisTitle(QwtPlot::yLeft,"Y->");ui->qwtPlot->setAxisTitle(QwtPlot::xBottom,"X->");//setAxisScale四个参数的含义分别是//坐标轴,//坐标轴最小值,//坐标轴最大值,//步进//左坐标轴 QwtPlot::yLeft//右坐标轴 QwtPlot::yRight//底部坐标轴 QwtPlot::xBottom//顶部坐标轴 QwtPlot::xTopQVector<QPointF> vector; //设置二维点信息for(int i=0; i<100; i++){QPointF point;point.setX(i); //用给定的坐标确定x坐标int y = 20*sin(i*M_PI/10) + 50;point.setY(y); //用给定的坐标确定y坐标vector.append(point);}//构造曲线数据QwtPointSeriesData* series = new QwtPointSeriesData(vector);//创建曲线curve1QwtPlotCurve* curve1 = new QwtPlotCurve("Curve 1");//设置数据curve1->setData(series);//设置画笔(颜色,粗细,线条样式)curve1->setPen(QColor(255,0,0),3,Qt::SolidLine);//设置填充画刷QBrush brush1(QColor(180,180,180));curve1->setBrush(brush1);//使曲线更光滑curve1->setCurveAttribute(QwtPlotCurve::Fitted, true);//加入网格QwtPlotGrid* grid = new QwtPlotGrid();grid->setPen(QColor(222,222,222),1);grid->attach(ui->qwtPlot);//把曲线附加到qwtPlot上curve1->attach(ui->qwtPlot);//绘制曲线ui->qwtPlot->replot();//ui界面显示曲线ui->qwtPlot->show();
}MainWindow::~MainWindow()
{delete ui;
}
最终效果图如下:
关于Qwt的使用-QwtPlot相关推荐
- Qwt使用之QwtPlot
QwtPlot是用来绘制二维图像的widget.在它的画板上可以无限制的显示绘画组件.绘画组件可以是曲线(QwtPlotCurve).标记(QwtPlotMarker).网格(QwtPlotGrid) ...
- QWT下载、安装、配置教程
一.关于Qt Creater的必要说明 1. minGW版的Qt Creater和msvc版的Qt Creater区别: 至于你自己用的是哪一个版本的Qt Creater可以通过帮助about Qt ...
- QWT错误static struct QMetaObject const QwtPlot
使用QWT例子oscilloscope出现如下错误: error LNK2001: unresolved external symbol "public: static struct QMe ...
- Qwt笔记(二)之QwtPlot简单使用
一.前言 文章只是楼主学习过程的记录,不能面面俱到,也不能每个点都讲到,不喜勿喷 二.QwtPlot是啥?? 文档是这样描述的: QwtPlot is a widget for plotting tw ...
- qt中label画圆代表指示灯_【Qt编程】基于QWT的曲线绘制及图例显示操作——有样点的实现功能...
在<QWT在QtCreator中的安装与使用>一文中,我们完成了QWT的安装,这篇文章我们讲讲基础曲线的绘制功能. 首先,我们新建一个Qt应用程序,然后一路默认即可.这时,你会发现总共有: ...
- 使用qwt作曲线图——有网格线背景的画法
创建自己的QwtPlot (1) 增加一个类比如curvePlotWidget,继承自QwtPlot (2)代码示例 1 curvePlotWidget::curvePlotWidget(QW ...
- Qwt中如何在曲线上标定特定点
发现 网上的资料越来越少了,只好自己看着QWT官方的Guidebook,一点点的摸索(手册写的真的有点表意不明) 首先要清楚QwtPlot是个容器类,就像一个画布一样 下面的代码就是在这个plot的为 ...
- qwt自定义时间标尺TimeScale
需要重载QwtScaleDraw类的virtual QwtText label(double)const;方法 该方法在需要绘制scale label时会调用. 主要思路是标尺上的点对应QDateTi ...
- qwt的安装和移植-
目须要依据实时数据绘制出图表,因此我们找到了qwt库.这个库是一个绘制图表,曲线图,柱状图的统计图标. . .. 以下我们就具体解说一下这个库在Larm上的编译和移植 qwt介绍 QWT,全称是Qt ...
最新文章
- FFT ---- 2021牛客多校第一场 H Hash Function
- 图像处理之基础---极坐标系及其与直角坐标系的关系
- python logging 毫秒级别的时间打印
- android获取天气信息_像素小天气
- 顶级俄国数学家是怎样炼成的?[2016-06-25 张羿 赛先生]
- Silverlight初级教程-库
- linux update语句,MySQL 多表 update sql语句总结
- AliOS Things手势识别应用演示
- 你不是颠覆 IoT,就是被 IoT 颠覆!
- python立即关机_“python怎么控制计算机关机“python自动关机教程
- servlet3.0理解
- json转数组(json数组对象)
- matlab闭环直流调速系统实验,实验三双闭环直流调速系统MATLAB仿真.doc
- Eclipse配置GitHub代码库(以Windows7为例)
- 彩色数字图像处理基础
- springboot--入门程序实现CRUD操作
- 简单excel饼状图怎么做,bi工具怎么做饼状图
- 云班课python答案_云班课测试题答案
- pc无线连接多台移动设备
- VsCode+OpenOCD 开发stm32系列