Qwt 基础的使用方法
来源:互联网 发布:运行windows激活信息的 编辑:程序博客网 时间:2024/05/20 06:29
本文转自“http://blog.chinaunix.net/uid-20717410-id-260233.html”
Qwt 基础的使用方法
p, li { white-space: pre-wrap; }鼠标滚轮放大缩小:
- QwtPlotMagnifier*PM= new QwtPlotMagnifier( ui->qwtPlot->canvas());
- QwtPlotPanner*PQ= new QwtPlotPanner( ui->qwtPlot->canvas());
- QwtPlotZoomer* zoomer = new QwtPlotZoomer( ui->qwtPlot->canvas() );
- zoomer->setRubberBandPen( QColor( Qt::black ) );
- zoomer->setTrackerPen( QColor( Qt::black ) );
- zoomer->setMousePattern(QwtEventPattern::MouseSelect2,Qt::RightButton, Qt::ControlModifier );
- zoomer->setMousePattern(QwtEventPattern::MouseSelect3,Qt::RightButton );
- ui->qwtPlot->setAxisTitle(QwtPlot::xBottom,"x -->" );
- ui->qwtPlot->setAxisScale(QwtPlot::xBottom, 0.0, 30.0);
- ui->qwtPlot->setAxisTitle(QwtPlot::yLeft,"y -->");
- ui->qwtPlot->setAxisScale(QwtPlot::yLeft,-1.0, 1.0);
- QwtPlotCurve*cSin= new QwtPlotCurve("y = sin(x)");
- cSin->setRenderHint(QwtPlotItem::RenderAntialiased);
- cSin->setLegendAttribute(QwtPlotCurve::LegendShowLine,true);
- cSin->setPen(QPen(Qt::blue));
- cSin->attach(ui->qwtPlot);
- cSin->setData(new FunctionData(::sin));
classFunctionData : publicQwtSyntheticPointData
{
public:
FunctionData(double(*y)(double)):QwtSyntheticPointData(100),d_y(y){}
virtualdoubley(doublex)const{returnd_y(x);}
private:double(*d_y)(double);};
- class FunctionData:public QwtSyntheticPointData
- {
- public:
- FunctionData(double(*y)(double)):
- QwtSyntheticPointData(100),
- d_y(y)
- {
- }
- virtual double y(double x)const
- {
- return d_y(x);
- }
- private:
- double(*d_y)(double);
- };
创建波形标识:(Y=0)
QwtPlotMarker*mY=newQwtPlotMarker();
mY->setLabel(QString::fromLatin1("y=0"));
mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
mY->setLineStyle(QwtPlotMarker::HLine);
mY->setYValue(0.0);
mY->attach(ui->qwtPlot);
- QwtPlotMarker*mY= new QwtPlotMarker();
- mY->setLabel(QString::fromLatin1("y = 0"));
- mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
- mY->setLineStyle(QwtPlotMarker::HLine);
- mY->setYValue(0.0);
- mY->attach(ui->qwtPlot);
创建波形标识:(X=PI/2)
QwtPlotMarker*mX=newQwtPlotMarker();
mX->setLabel(QString::fromLatin1("x=PI/2"));
mX->setLabelAlignment(Qt::AlignLeft|Qt::AlignBottom);
mX->setLabelOrientation(Qt::Vertical);
mX->setLineStyle(QwtPlotMarker::VLine);
mX->setLinePen(QPen(Qt::black,1,Qt::DashDotDotLine));
mX->setXValue(M_PI/2);
mX->attach(ui->qwtPlot);
- QwtPlotMarker*mX= new QwtPlotMarker();
- mX->setLabel(QString::fromLatin1("x = PI/2"));
- mX->setLabelAlignment(Qt::AlignLeft| Qt::AlignBottom);
- mX->setLabelOrientation(Qt::Vertical);
- mX->setLineStyle(QwtPlotMarker::VLine);
- mX->setLinePen(QPen(Qt::black, 1, Qt::DashDotDotLine));
- mX->setXValue(M_PI/2);
- mX->attach(ui->qwtPlot);
设置qwtPlot的画布:(圆角矩形)
ui->qwtPlot->canvas()->setLineWidth(1);
ui->qwtPlot->canvas()->setFrameStyle(QFrame::Box|QFrame::Plain);
ui->qwtPlot->canvas()->setBorderRadius(15);
- ui->qwtPlot->canvas()->setLineWidth( 1 );
- ui->qwtPlot->canvas()->setFrameStyle( QFrame::Box| QFrame::Plain);
- ui->qwtPlot->canvas()->setBorderRadius( 15 );
设置qwtPlot的画布:(白色填充)
QPalettecanvasPalette(Qt::white);
canvasPalette.setColor(QPalette::Foreground,QColor(133,190,232));
ui->qwtPlot->canvas()->setPalette(canvasPalette);
- QPalette canvasPalette( Qt::white);
- canvasPalette.setColor( QPalette::Foreground, QColor( 133, 190, 232));
- ui->qwtPlot->canvas()->setPalette( canvasPalette );
设置整个界面的颜色:
QPalettepal=palette();
constQColorbuttonColor=pal.color(QPalette::Button);
QLinearGradientgradient(0,0,0,1);
gradient.setCoordinateMode(QGradient::StretchToDeviceMode);
gradient.setColorAt(0.0,Qt::white);
gradient.setColorAt(0.7,buttonColor);
gradient.setColorAt(1.0,buttonColor);
pal.setBrush(QPalette::Window,gradient);
setPalette(pal);
- QPalette pal= palette();
- const QColor buttonColor= pal.color( QPalette::Button);
- QLinearGradient gradient( 0, 0, 0, 1);
- gradient.setCoordinateMode( QGradient::StretchToDeviceMode);
- gradient.setColorAt( 0.0, Qt::white);
- gradient.setColorAt( 0.7, buttonColor);
- gradient.setColorAt( 1.0, buttonColor);
- pal.setBrush( QPalette::Window, gradient );
- setPalette( pal);
Qt绘图 使用QWT绘制科学图表、绘图
Qwt是一个基于LGPL版权协议的开源项目,其目标是提供一组2D的窗体库显示技术领域的数据,数据源以浮点数组或范围的方式提供,输出方式可以是Curves(曲线),Slider(滚动条),Dials(圆盘),compasses(仪表盘)等等。该工具库基于Qt开发,所以也继承了Qt的跨平台特性,据原作者文档所说,该项目在Qt-win/Qt-x11/Qt-embedded(qvfb环境)上都测试过,运行正常。 项目的主页在:http://qwt.sourceforge.net/
在网上搜了一下,发现关于qwt的中文资料实在很少,基本上只有关于编译和安装的文章。 实际上经过笔者实践,Qwt的编译和运行实在乏善可陈,qmake;make就可以搞定,没什么可memo的东西,所以这篇文章将以扫盲为主, 介绍Qwt的feature。
当然按照正常的顺序,我们还是从编译安装开始。 从svn服务器上下载最新代码:svncohttps://qwt.svn.sourceforge.net/svnroot/qwt/trunk/qwt
进入qwt目录,运行你电脑上qt4对应的qmake,再运行make编译。如笔者环境中是
$cd qwt
$ export PATH=/usr/local/Trolltech/Qt-4.5.1/bin/:$PATH
$qmake
$ make
编译要花个几分钟的时间。成功后在lib下会生成libqwt.so*文件,并且examples也参与编译,生成的binary在examples/bin下,我们可以运行这些例子初步查看qwt的功能。
$export LD_LIBRARY_PATH=$PWD/lib
$cdexamples/bin
$ ./simple
simple是qwt自带的例子中最简单的一个,一共只有一百来行的代码,实现了数学中的正弦函数(sin())和余弦函数(cos())曲线。如下图:
这个例子里用到的核心类有四个(以下内容是笔者的理解,有可能有不对的地方, 请酌情阅读):
QwtPlot
类似一个2D绘图的容器,里面可以放其他的QwtPlotItem派生类对象,比如本例子中使用的QwtPlotMarker等。(有点类似Qt里的graphicsview的感觉。)这个类负责控制绘图数据的产生和传递,并且绘制一个坐标轴。
QwtPlotMarker
标尺类,用于绘制刻度线。
QwtPlotCurve
曲线类,用于绘制各种曲线。
QwtSyntheticPointData
比较奇怪,这是个undocumented的类,估计不小心被作者遗漏了, 文档中没有提供该类的说明,只能从源码中寻找答案了, 这一点很让人郁闷。通过看code当中的注释和例子代码,笔者的理解是该类负责产生数据, 它的作用是提供固定数量的浮点数的点,点的数量在构造类时传入。获取数据时QwtPlotCurve类会调用该类的y()方法获取纵坐标。
程序的基本流程是:
1、初始化绘图容器,设置坐标轴的参数
//Set axes
setAxisTitle(xBottom, “x–>”);
setAxisScale(xBottom, 0.0,10.0);//横坐标从0到10,xBottom表示横坐标的方向从下往上
setAxisTitle(yLeft,“y –>”);
setAxisScale(yLeft, -1.0,1.0);//纵坐标-1到1,yLeft表示纵坐标的方向从左到右
2、添加正弦余弦曲线
//Insert new curves
QwtPlotCurve *cSin = new QwtPlotCurve(”y =sin(x)”);
#if QT_VERSION >=0×040000
cSin->setRenderHint(QwtPlotItem::RenderAntialiased);
#endif
cSin->setPen(QPen(Qt::red));//红色曲线
cSin->attach(this);
QwtPlotCurve*cCos = new QwtPlotCurve(”y = cos(x)”);
#if QT_VERSION >=0×040000
cCos->setRenderHint(QwtPlotItem::RenderAntialiased);
#endif
cCos->setPen(QPen(Qt::blue));//蓝色曲线
cCos->attach(this);
3、设置曲线的数据内容
//Create sin and cosdata
cSin->setData(FunctionData(::sin));
cCos->setData(FunctionData(::cos));
4、添加横纵标尺线作为坐标的参照
//Insert markers
// …a horizontal line at y = 0…
QwtPlotMarker*mY = new QwtPlotMarker();
mY->setLabel(QString::fromLatin1(”y=0″));
mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
mY->setLineStyle(QwtPlotMarker::HLine);//线的方向
mY->setYValue(0.0);//标尺线画在y=0的位置
mY->attach(this);
//…a vertical line at x = 2 * pi
QwtPlotMarker *mX = newQwtPlotMarker();
mX->setLabel(QString::fromLatin1(”x = 2pi”));
mX->setLabelAlignment(Qt::AlignLeft |Qt::AlignBottom);
mX->setLabelOrientation(Qt::Vertical);
mX->setLineStyle(QwtPlotMarker::VLine);
mX->setLinePen(QPen(Qt::black,0, Qt::DashDotLine));
mX->setXValue(2.0* M_PI);//该标线画在x=2PI的位置
mX->attach(this);
- Qwt 基础的使用方法
- QWT类的一些使用方法
- QWT类的一些使用方法
- QWT类的一些使用方法
- 【小计】QWT类的一些使用方法
- Qt:QWT类的一些使用方法
- UITextField的基础使用方法
- Eric6的基础使用方法
- ToolBar的基础使用方法
- Dialog 基础的使用方法
- SharedPreference的基础使用方法
- QWT的安装
- QWT的使用陷阱
- Qt qwt的example
- QWT 程序的移植
- qwt 的一些使用
- QWT坐标轴的设置
- QWT的使用
- js限制输入文字长度
- iOS: 对图片进行不规则剪裁
- Perl-11---循环修饰符
- 黄金数据查询演示示例
- CSS3.0盒模型display:-webkit-box;的使用
- Qwt 基础的使用方法
- Python-multiprocessing进程管理
- Linux下解压缩操作
- jpg转换成pdf破解工具
- swing入门教程
- RPC or REST in the Cloud?
- 模板的偏特化
- svn更改用户名
- 编写Makefile文件