Qwt 基础的使用方法

来源:互联网 发布:运行windows激活信息的 编辑:程序博客网 时间:2024/05/20 06:29

本文转自“http://blog.chinaunix.net/uid-20717410-id-260233.html”


Qwt 基础的使用方法

p, li { white-space: pre-wrap; }鼠标滚轮放大缩小:

  1. QwtPlotMagnifier*PM= new QwtPlotMagnifier( ui->qwtPlot->canvas());
鼠标左键拖动波形:
  1. QwtPlotPanner*PQ= new QwtPlotPanner( ui->qwtPlot->canvas());
鼠标左键选择区域放大:(右键还原)
  1. QwtPlotZoomer* zoomer = new QwtPlotZoomer( ui->qwtPlot->canvas() );
  2. zoomer->setRubberBandPen( QColor( Qt::black ) );
  3. zoomer->setTrackerPen( QColor( Qt::black ) );
  4. zoomer->setMousePattern(QwtEventPattern::MouseSelect2,Qt::RightButton, Qt::ControlModifier );
  5. zoomer->setMousePattern(QwtEventPattern::MouseSelect3,Qt::RightButton );
设置X轴下面标识:
  1. ui->qwtPlot->setAxisTitle(QwtPlot::xBottom,"x -->" );
设置X轴取值范围:
  1. ui->qwtPlot->setAxisScale(QwtPlot::xBottom, 0.0, 30.0);
设置Y轴左边标识(竖着显示):
  1. ui->qwtPlot->setAxisTitle(QwtPlot::yLeft,"y -->");
设置Y轴取值范围:
  1. ui->qwtPlot->setAxisScale(QwtPlot::yLeft,-1.0, 1.0);
创建一个sin()曲线:
  1. QwtPlotCurve*cSin= new QwtPlotCurve("y = sin(x)");
  2. cSin->setRenderHint(QwtPlotItem::RenderAntialiased);
  3. cSin->setLegendAttribute(QwtPlotCurve::LegendShowLine,true);
  4. cSin->setPen(QPen(Qt::blue));
  5.  cSin->attach(ui->qwtPlot);
  6. cSin->setData(new FunctionData(::sin));
其中FunctionData为:

classFunctionData : publicQwtSyntheticPointData

{

public:

FunctionData(double(*y)(double)):QwtSyntheticPointData(100),d_y(y){}

virtualdoubley(doublex)const{returnd_y(x);}

private:double(*d_y)(double);};

  1. class FunctionData:public QwtSyntheticPointData
  2. {
  3. public:
  4.     FunctionData(double(*y)(double)):
  5.         QwtSyntheticPointData(100),
  6.         d_y(y)
  7.     {
  8.     }
  9.     virtual double y(double x)const
  10.     {
  11.         return d_y(x);
  12.     }
  13. private:
  14.     double(*d_y)(double);
  15. };

创建波形标识:(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);

  1. QwtPlotMarker*mY= new QwtPlotMarker();
  2. mY->setLabel(QString::fromLatin1("y = 0"));
  3. mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
  4. mY->setLineStyle(QwtPlotMarker::HLine);
  5. mY->setYValue(0.0);
  6. 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);

  1. QwtPlotMarker*mX= new QwtPlotMarker();
  2. mX->setLabel(QString::fromLatin1("x = PI/2"));
  3. mX->setLabelAlignment(Qt::AlignLeft| Qt::AlignBottom);
  4. mX->setLabelOrientation(Qt::Vertical);
  5. mX->setLineStyle(QwtPlotMarker::VLine);
  6. mX->setLinePen(QPen(Qt::black, 1, Qt::DashDotDotLine));
  7. mX->setXValue(M_PI/2);
  8. mX->attach(ui->qwtPlot);

设置qwtPlot的画布:(圆角矩形)

ui->qwtPlot->canvas()->setLineWidth(1);

ui->qwtPlot->canvas()->setFrameStyle(QFrame::Box|QFrame::Plain);

ui->qwtPlot->canvas()->setBorderRadius(15);

  1. ui->qwtPlot->canvas()->setLineWidth( 1 );
  2. ui->qwtPlot->canvas()->setFrameStyle( QFrame::Box| QFrame::Plain);
  3. ui->qwtPlot->canvas()->setBorderRadius( 15 );

设置qwtPlot的画布:(白色填充)

QPalettecanvasPalette(Qt::white);

canvasPalette.setColor(QPalette::Foreground,QColor(133,190,232));

ui->qwtPlot->canvas()->setPalette(canvasPalette);

  1. QPalette canvasPalette( Qt::white);
  2. canvasPalette.setColor( QPalette::Foreground, QColor( 133, 190, 232));
  3. 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);

  1. QPalette pal= palette();
  2. const QColor buttonColor= pal.color( QPalette::Button);
  3. QLinearGradient gradient( 0, 0, 0, 1);
  4. gradient.setCoordinateMode( QGradient::StretchToDeviceMode);
  5. gradient.setColorAt( 0.0, Qt::white);
  6. gradient.setColorAt( 0.7, buttonColor);
  7. gradient.setColorAt( 1.0, buttonColor);
  8. pal.setBrush( QPalette::Window, gradient );
  9. setPalette( pal);



Qt绘图 使用QWT绘制科学图表、绘图

Qwt是一个基于LGPL版权协议的开源项目,其目标是提供一组2D的窗体库显示技术领域的数据,数据源以浮点数组或范围的方式提供,输出方式可以是Curves(曲线),Slider(滚动条),Dials(圆盘),compasses(仪表盘)等等。该工具库基于Qt开发,所以也继承了Qt的跨平台特性,据原作者文档所说,该项目在Qt-win/Qt-x11/Qt-embeddedqvfb环境)上都测试过,运行正常。 项目的主页在:http://qwt.sourceforge.net/

在网上搜了一下,发现关于qwt的中文资料实在很少,基本上只有关于编译和安装的文章。 实际上经过笔者实践,Qwt的编译和运行实在乏善可陈,qmake;make就可以搞定,没什么可memo的东西,所以这篇文章将以扫盲为主, 介绍Qwtfeature

当然按照正常的顺序,我们还是从编译安装开始。 从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也参与编译,生成的binaryexamples/bin下,我们可以运行这些例子初步查看qwt的功能。

$export LD_LIBRARY_PATH=$PWD/lib
$cdexamples/bin
$ ./simple

simpleqwt自带的例子中最简单的一个,一共只有一百来行的代码,实现了数学中的正弦函数(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);//
横坐标从010xBottom表示横坐标的方向从下往上

setAxisTitle(yLeft,“y –>”);
setAxisScale(
yLeft, -1.0,1.0);//
纵坐标-11yLeft表示纵坐标的方向从左到右

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);//
标尺线画在y0的位置
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);//
该标线画在x2PI的位置
mX->attach(this);

0 0
原创粉丝点击