Qwt例子-CpuPlot注释版
来源:互联网 发布:实验数据记录怎么写 编辑:程序博客网 时间:2024/05/01 04:39
初学QWt的话,里面有很多例子,一开始读起来还是挺难懂的。特此注释一番,方便后续理解。
1.1 CpuPlot.h文件
- #include <qwt_plot.h>
- #include "cpustat.h"
- #define HISTORY 60 // seconds
- class QwtPlotCurve;
- class CpuPlot :public QwtPlot
- {
- Q_OBJECT
- public:
- enum CpuData
- {
- User,
- System,
- Total,
- Idle,
- NCpuData
- };
- CpuPlot( QWidget * = 0 );
- const QwtPlotCurve *cpuCurve(int id ) const
- {
- return data[id].curve;
- }
- protected:
- void timerEvent( QTimerEvent *e );
- private Q_SLOTS:
- void legendChecked(const QVariant &, bool on );
- private:
- void showCurve( QwtPlotItem *,bool on );
- struct
- {
- QwtPlotCurve *curve;
- double data[HISTORY];
- } data[NCpuData];
- double timeData[HISTORY];
- int dataCount;
- CpuStat cpuStat;
- };
1.2 CpuPlot.cpp文件
- #include <qapplication.h>
- #include <qlayout.h>
- #include <qlabel.h>
- #include <qpainter.h>
- #include <qwt_plot_layout.h>
- #include <qwt_plot_curve.h>
- #include <qwt_scale_draw.h>
- #include <qwt_scale_widget.h>
- #include <qwt_legend.h>
- #include <qwt_legend_label.h>
- #include <qwt_plot_canvas.h>
- #include "cpupiemarker.h"
- #include "cpuplot.h"
- //时间刻度表
- class TimeScaleDraw:public QwtScaleDraw
- {
- public:
- TimeScaleDraw( const QTime &base ):
- baseTime( base )
- {
- }
- virtual QwtText label(double v ) const
- {
- QTime upTime = baseTime.addSecs( static_cast<int>( v ) );
- return upTime.toString();
- }
- private:
- QTime baseTime;
- };
- //背景色填充
- class Background:public QwtPlotItem
- {
- public:
- Background()
- {
- setZ( 0.0 );
- }
- virtualint rtti() const
- {
- return QwtPlotItem::Rtti_PlotUserItem;
- }
- virtualvoid draw( QPainter *painter,
- const QwtScaleMap &,const QwtScaleMap &yMap,
- const QRectF &canvasRect )const
- {
- QColor c( Qt::white );
- QRectF r = canvasRect;
- for (int i = 100; i > 0; i -= 10 )
- {
- r.setBottom( yMap.transform( i - 10 ) );//将相对于Y坐标轴值 转换成相对于窗口绘图设备的相对值
- r.setTop( yMap.transform( i ) );
- painter->fillRect( r, c );//填充矩形背景
- c = c.dark( 110 );//递增变黑,但该方法已经过时了,Qt5.3应该是 c.darker()
- }
- }
- };
- //曲线
- class CpuCurve:public QwtPlotCurve
- {
- public:
- CpuCurve( const QString &title ):
- QwtPlotCurve( title )
- {
- setRenderHint( QwtPlotItem::RenderAntialiased );//设置渲染效果,抗锯齿
- }
- void setColor(const QColor &color )
- {
- QColor c = color;
- c.setAlpha( 150 );//设置Alpha通道颜色
- setPen( c );//设置画笔
- setBrush( c );//设置笔刷
- }
- };
- CpuPlot::CpuPlot( QWidget *parent ):
- QwtPlot( parent ),
- dataCount( 0 )
- {
- setAutoReplot( false );
- QwtPlotCanvas *canvas = new QwtPlotCanvas();//画布
- canvas->setBorderRadius( 10 );//画布四周边框角半径
- setCanvas( canvas );//设置绘图的画布
- plotLayout()->setAlignCanvasToScales( true );//设置对齐画布、坐标轴、刻度
- QwtLegend *legend = new QwtLegend;//曲线描述
- legend->setDefaultItemMode( QwtLegendData::Checkable );//设置描述是QCheckBox类型
- insertLegend( legend, QwtPlot::RightLegend );//插入位置,可以分别是上下左右
- setAxisTitle( QwtPlot::xBottom, " System Uptime [h:m:s]" );//坐标轴标题-x轴
- setAxisScaleDraw( QwtPlot::xBottom,
- new TimeScaleDraw( cpuStat.upTime() ) );//设置x轴的刻度绘画
- setAxisScale( QwtPlot::xBottom, 0, HISTORY );//设置x轴坐标刻度大小
- setAxisLabelRotation( QwtPlot::xBottom, -50.0 );//标签旋转50度
- setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
- /*
- In situations, when there is a label at the most right position of the
- scale, additional space is needed to display the overlapping part
- of the label would be taken by reducing the width of scale and canvas.
- To avoid this "jumping canvas" effect, we add a permanent margin.
- We don't need to do the same for the left border, because there
- is enough space for the overlapping label below the left scale.
- */
- QwtScaleWidget *scaleWidget = axisWidget( QwtPlot::xBottom );//x轴刻度控件
- constint fmh = QFontMetrics( scaleWidget->font() ).height();
- scaleWidget->setMinBorderDist( 0, fmh / 2 );//设置刻度的边界最小值,防止因为标签变化,引起跳动
- setAxisTitle( QwtPlot::yLeft, "Cpu Usage [%]" );//Y轴设置标题
- setAxisScale( QwtPlot::yLeft, 0, 100 );//Y轴刻度
- Background *bg = new Background();//给绘图填充背景
- bg->attach( this );//插入plot
- CpuPieMarker *pie = new CpuPieMarker();//用户自定义的标记QwtPlotMarker
- pie->attach( this );//插入plot
- CpuCurve *curve;//Cpu曲线
- curve = new CpuCurve( "System" );
- curve->setColor( Qt::red );//设置曲线颜色
- curve->attach( this );//插入plot
- data[System].curve = curve;
- curve = new CpuCurve( "User" );
- curve->setColor( Qt::blue );
- curve->setZ( curve->z() - 1 );//设置z轴上下先后顺序,哪个曲线在上。
- curve->attach( this );
- data[User].curve = curve;
- curve = new CpuCurve( "Total" );
- curve->setColor( Qt::black );
- curve->setZ( curve->z() - 2 );
- curve->attach( this );
- data[Total].curve = curve;
- curve = new CpuCurve( "Idle" );
- curve->setColor( Qt::darkCyan );
- curve->setZ( curve->z() - 3 );
- curve->attach( this );
- data[Idle].curve = curve;
- showCurve( data[System].curve, true );//显示哪个曲线
- showCurve( data[User].curve, true );
- showCurve( data[Total].curve, false );
- showCurve( data[Idle].curve, false );
- for (int i = 0; i < HISTORY; i++ )
- timeData[HISTORY - 1 - i] = i;
- ( void )startTimer( 1000 );// 1 second 启动定时器,timerEvent会1s响应一次。(timerId相对应的定时器)
- connect( legend, SIGNAL( checked( const QVariant &, bool,int ) ),
- SLOT( legendChecked( const QVariant &, bool ) ) );//控制曲线显示,注意传递的参数类型
- }
- //定时器执行的代码
- void CpuPlot::timerEvent( QTimerEvent * )
- {
- for (int i = dataCount; i > 0; i-- )
- {
- for (int c = 0; c < NCpuData; c++ )
- {
- if ( i < HISTORY )
- data[c].data[i] = data[c].data[i-1];
- }
- }
- cpuStat.statistic( data[User].data[0], data[System].data[0] );
- data[Total].data[0] = data[User].data[0] + data[System].data[0];
- data[Idle].data[0] = 100.0 - data[Total].data[0];
- if ( dataCount < HISTORY )
- dataCount++;
- for (int j = 0; j < HISTORY; j++ )
- timeData[j]++;
- setAxisScale( QwtPlot::xBottom,
- timeData[HISTORY - 1], timeData[0] );//每次设置一下x轴刻度,起到递进变化效果
- for (int c = 0; c < NCpuData; c++ )
- {
- data[c].curve->setRawSamples(
- timeData, data[c].data, dataCount );//每次更新一下曲线值
- }
- replot();
- }
- void CpuPlot::legendChecked(const QVariant &itemInfo, bool on )
- {
- QwtPlotItem *plotItem = infoToItem( itemInfo );
- //如果选中,即显示该曲线
- if ( plotItem )
- showCurve( plotItem, on );
- }
- //控制一下哪个曲线显示
- void CpuPlot::showCurve( QwtPlotItem *item,bool on )
- {
- item->setVisible( on );
- //获取该绘图的描述
- QwtLegend *lgd = qobject_cast<QwtLegend *>( legend() );
- //获取所有描述组件
- QList<QWidget *> legendWidgets =
- lgd->legendWidgets( itemToInfo( item ) );
- if ( legendWidgets.size() == 1 )
- {
- //获取这个描述组件的标签值
- QwtLegendLabel *legendLabel =
- qobject_cast<QwtLegendLabel *>( legendWidgets[0] );
- //如果标签有值,即显示
- if ( legendLabel )
- legendLabel->setChecked( on );
- }
- replot();
- }
- int main(int argc, char **argv )
- {
- QApplication a( argc, argv );
- QWidget vBox;
- vBox.setWindowTitle( "Cpu Plot" );
- CpuPlot *plot = new CpuPlot( &vBox );
- plot->setTitle( "History" );
- constint margin = 5;
- plot->setContentsMargins( margin, margin, margin, margin );//页边距
- QString info( "Press the legend to en/disable a curve" );
- QLabel *label = new QLabel( info, &vBox );
- QVBoxLayout *layout = new QVBoxLayout( &vBox );
- layout->addWidget( plot );
- layout->addWidget( label );
- vBox.resize( 600, 400 );
- vBox.show();
- return a.exec();
- }
2.1CpuPieMarker.h
- //-----------------------------------------------------------------
- // This class shows how to extend QwtPlotItems. It displays a
- // pie chart of user/total/idle cpu usage in percent.
- //-----------------------------------------------------------------
- #include <qwt_plot_item.h>
- class CpuPieMarker:public QwtPlotItem
- {
- public:
- CpuPieMarker();
- virtualint rtti() const;
- virtualvoid draw( QPainter *,
- const QwtScaleMap &,const QwtScaleMap &, const QRectF & ) const;
- };
2.2 CpuPieMarker.cpp
- #include <qpainter.h>
- #include <qwt_scale_map.h>
- #include <qwt_plot_curve.h>
- #include "cpuplot.h"
- #include "cpupiemarker.h"
- //扇形状态标识
- CpuPieMarker::CpuPieMarker()
- {
- setZ( 1000 );
- setRenderHint( QwtPlotItem::RenderAntialiased, true );
- }
- //Run-Time Type Information
- int CpuPieMarker::rtti()const
- {
- return QwtPlotItem::Rtti_PlotUserItem;//用户自定义的
- }
- void CpuPieMarker::draw( QPainter *painter,
- const QwtScaleMap &,const QwtScaleMap &,
- const QRectF &rect )const
- {
- //获取当前attached的plot
- const CpuPlot *cpuPlot =static_cast<CpuPlot *> ( plot() );
- //获取当前Y轴的坐标映射关系图
- const QwtScaleMap yMap = cpuPlot->canvasMap( QwtPlot::yLeft );
- constint margin = 5;
- QRectF pieRect;//扇形矩阵方位和大小
- pieRect.setX( rect.x() + margin );//获得当前Marker需要显示的坐标
- pieRect.setY( rect.y() + margin );
- pieRect.setHeight( yMap.transform( 80.0 ) );//通过yMap映射,把正常值,设置成绘图的相对高度
- pieRect.setWidth( pieRect.height() );//高宽度一样
- constint dataType[] = { CpuPlot::User, CpuPlot::System, CpuPlot::Idle };
- int angle =static_cast<int>( 5760 * 0.75 );//开始值16*360*0.75 即在3/4角度 290度
- for ( unsignedint i = 0;
- i < sizeof( dataType ) / sizeof( dataType[0] ); i++ )
- {
- const QwtPlotCurve *curve = cpuPlot->cpuCurve( dataType[i] );//当前指定的曲线
- if ( curve->dataSize() > 0 )
- {
- const int value = static_cast<int>( 5760 * curve->sample( 0 ).y() / 100.0 );//当前
- //我们可以先利用save()函数来保存坐标系现在的状态,然后进行变换操作,操作完之后,再用restore()函数将以前的坐标系状态恢复,其实就是一个入栈和出栈的操作。
- //利用好这两个函数,可以实现快速的坐标系切换,绘制出不同的图形。
- painter->save();//保存坐标系状态
- painter->setBrush( QBrush( curve->pen().color(), Qt::SolidPattern ) );
- if ( value != 0 )
- painter->drawPie( pieRect, -angle, -value );//画扇形
- painter->restore();//恢复以前的坐标系状态
- angle += value;
- }
- }
- }
3.1 CpuStat.h
- #include <qdatetime.h>
- class CpuStat
- {
- public:
- CpuStat();
- void statistic(double &user, double &system );
- QTime upTime() const;
- enum Value
- {
- User,
- Nice,
- System,
- Idle,
- NValues
- };
- private:
- void lookUp(double[NValues] ) const;
- double procValues[NValues];
- };
3.2 CpuStat.cpp
- #include <qstringlist.h>
- #include <qfile.h>
- #include <qtextstream.h>
- #include "cpustat.h"
- CpuStat::CpuStat()
- {
- lookUp( procValues );
- }
- QTime CpuStat::upTime() const
- {
- QTime t( 0, 0, 0 );
- for (int i = 0; i < NValues; i++ )
- t = t.addSecs( int( procValues[i] / 100 ) );
- return t;
- }
- void CpuStat::statistic(double &user, double &system )
- {
- double values[NValues];
- lookUp( values );
- //userdata曲线值
- double userDelta = values[User] + values[Nice]
- - procValues[User] - procValues[Nice];
- //systemData曲线值
- double systemDelta = values[System] - procValues[System];
- //总计值
- double totalDelta = 0;
- for (int i = 0; i < NValues; i++ )
- totalDelta += values[i] - procValues[i];
- user = userDelta / totalDelta * 100.0;
- system = systemDelta / totalDelta * 100.0;
- for (int j = 0; j < NValues; j++ )
- procValues[j] = values[j];
- }
- void CpuStat::lookUp(double values[NValues] ) const
- {
- QFile file( "/proc/stat" );
- //若是不能打开linux的进程监控文件,自定义模拟数据
- if ( !file.open( QIODevice::ReadOnly ) )
- {
- staticdouble dummyValues[][NValues] =
- {
- { 103726, 0, 23484, 819556 },
- { 103783, 0, 23489, 819604 },
- { 103798, 0, 23490, 819688 },
- { 103820, 0, 23490, 819766 },
- { 103840, 0, 23493, 819843 },
- { 103875, 0, 23499, 819902 },
- { 103917, 0, 23504, 819955 },
- { 103950, 0, 23508, 820018 },
- { 103987, 0, 23510, 820079 },
- { 104020, 0, 23513, 820143 },
- { 104058, 0, 23514, 820204 },
- { 104099, 0, 23520, 820257 },
- { 104121, 0, 23525, 820330 },
- { 104159, 0, 23530, 820387 },
- { 104176, 0, 23534, 820466 },
- { 104215, 0, 23538, 820523 },
- { 104245, 0, 23541, 820590 },
- { 104267, 0, 23545, 820664 },
- { 104311, 0, 23555, 820710 },
- { 104355, 0, 23565, 820756 },
- { 104367, 0, 23567, 820842 },
- { 104383, 0, 23572, 820921 },
- { 104396, 0, 23577, 821003 },
- { 104413, 0, 23579, 821084 },
- { 104446, 0, 23588, 821142 },
- { 104521, 0, 23594, 821161 },
- { 104611, 0, 23604, 821161 },
- { 104708, 0, 23607, 821161 },
- { 104804, 0, 23611, 821161 },
- { 104895, 0, 23620, 821161 },
- { 104993, 0, 23622, 821161 },
- { 105089, 0, 23626, 821161 },
- { 105185, 0, 23630, 821161 },
- { 105281, 0, 23634, 821161 },
- { 105379, 0, 23636, 821161 },
- { 105472, 0, 23643, 821161 },
- { 105569, 0, 23646, 821161 },
- { 105666, 0, 23649, 821161 },
- { 105763, 0, 23652, 821161 },
- { 105828, 0, 23661, 821187 },
- { 105904, 0, 23666, 821206 },
- { 105999, 0, 23671, 821206 },
- { 106094, 0, 23676, 821206 },
- { 106184, 0, 23686, 821206 },
- { 106273, 0, 23692, 821211 },
- { 106306, 0, 23700, 821270 },
- { 106341, 0, 23703, 821332 },
- { 106392, 0, 23709, 821375 },
- { 106423, 0, 23715, 821438 },
- { 106472, 0, 23721, 821483 },
- { 106531, 0, 23727, 821517 },
- { 106562, 0, 23732, 821582 },
- { 106597, 0, 23736, 821643 },
- { 106633, 0, 23737, 821706 },
- { 106666, 0, 23742, 821768 },
- { 106697, 0, 23744, 821835 },
- { 106730, 0, 23748, 821898 },
- { 106765, 0, 23751, 821960 },
- { 106799, 0, 23754, 822023 },
- { 106831, 0, 23758, 822087 },
- { 106862, 0, 23761, 822153 },
- { 106899, 0, 23763, 822214 },
- { 106932, 0, 23766, 822278 },
- { 106965, 0, 23768, 822343 },
- { 107009, 0, 23771, 822396 },
- { 107040, 0, 23775, 822461 },
- { 107092, 0, 23780, 822504 },
- { 107143, 0, 23787, 822546 },
- { 107200, 0, 23795, 822581 },
- { 107250, 0, 23803, 822623 },
- { 107277, 0, 23810, 822689 },
- { 107286, 0, 23810, 822780 },
- { 107313, 0, 23817, 822846 },
- { 107325, 0, 23818, 822933 },
- { 107332, 0, 23818, 823026 },
- { 107344, 0, 23821, 823111 },
- { 107357, 0, 23821, 823198 },
- { 107368, 0, 23823, 823284 },
- { 107375, 0, 23824, 823377 },
- { 107386, 0, 23825, 823465 },
- { 107396, 0, 23826, 823554 },
- { 107422, 0, 23830, 823624 },
- { 107434, 0, 23831, 823711 },
- { 107456, 0, 23835, 823785 },
- { 107468, 0, 23838, 823870 },
- { 107487, 0, 23840, 823949 },
- { 107515, 0, 23843, 824018 },
- { 107528, 0, 23846, 824102 },
- { 107535, 0, 23851, 824190 },
- { 107548, 0, 23853, 824275 },
- { 107562, 0, 23857, 824357 },
- { 107656, 0, 23863, 824357 },
- { 107751, 0, 23868, 824357 },
- { 107849, 0, 23870, 824357 },
- { 107944, 0, 23875, 824357 },
- { 108043, 0, 23876, 824357 },
- { 108137, 0, 23882, 824357 },
- { 108230, 0, 23889, 824357 },
- { 108317, 0, 23902, 824357 },
- { 108412, 0, 23907, 824357 },
- { 108511, 0, 23908, 824357 },
- { 108608, 0, 23911, 824357 },
- { 108704, 0, 23915, 824357 },
- { 108801, 0, 23918, 824357 },
- { 108891, 0, 23928, 824357 },
- { 108987, 0, 23932, 824357 },
- { 109072, 0, 23943, 824361 },
- { 109079, 0, 23943, 824454 },
- { 109086, 0, 23944, 824546 },
- { 109098, 0, 23950, 824628 },
- { 109108, 0, 23955, 824713 },
- { 109115, 0, 23957, 824804 },
- { 109122, 0, 23958, 824896 },
- { 109132, 0, 23959, 824985 },
- { 109142, 0, 23961, 825073 },
- { 109146, 0, 23962, 825168 },
- { 109153, 0, 23964, 825259 },
- { 109162, 0, 23966, 825348 },
- { 109168, 0, 23969, 825439 },
- { 109176, 0, 23971, 825529 },
- { 109185, 0, 23974, 825617 },
- { 109193, 0, 23977, 825706 },
- { 109198, 0, 23978, 825800 },
- { 109206, 0, 23978, 825892 },
- { 109212, 0, 23981, 825983 },
- { 109219, 0, 23981, 826076 },
- { 109225, 0, 23981, 826170 },
- { 109232, 0, 23984, 826260 },
- { 109242, 0, 23984, 826350 },
- { 109255, 0, 23986, 826435 },
- { 109268, 0, 23987, 826521 },
- { 109283, 0, 23990, 826603 },
- { 109288, 0, 23991, 826697 },
- { 109295, 0, 23993, 826788 },
- { 109308, 0, 23994, 826874 },
- { 109322, 0, 24009, 826945 },
- { 109328, 0, 24011, 827037 },
- { 109338, 0, 24012, 827126 },
- { 109347, 0, 24012, 827217 },
- { 109354, 0, 24017, 827305 },
- { 109367, 0, 24017, 827392 },
- { 109371, 0, 24019, 827486 },
- };
- staticint counter = 0;
- //将dummyValues初始化为values一维数组
- for (int i = 0; i < NValues; i++ )
- values[i] = dummyValues[counter][i];
- counter = ( counter + 1 )
- % ( sizeof( dummyValues ) / sizeof( dummyValues[0] ) );
- }
- else//打开linux系统真实数据
- {
- QTextStream textStream( &file );
- do
- {
- QString line = textStream.readLine();
- line = line.trimmed();
- if ( line.startsWith( "cpu " ) )
- {
- const QStringList valueList =
- line.split( " ", QString::SkipEmptyParts );
- if ( valueList.count() >= 5 )
- {
- for ( int i = 0; i < NValues; i++ )
- values[i] = valueList[i+1].toDouble();
- }
- break;
- }
- }
- while( !textStream.atEnd() );
- }
- }
0 0
- Qwt例子-CpuPlot注释版
- Qwt直方图例子注释
- qt组直方图例子-qwt
- QWT
- QWT
- qwt
- R语言帮助例子注释版:hist()
- Doxygen注释例子
- osgAnimation 例子 注释
- lucence小例子-注释详解
- 一个自定义注释的例子
- eclipse注释模板导入例子
- 一个自定义注释的例子
- JAVA注释应用小例子
- 《PyQt5 快速编程》例子注释
- JXTA的一个例子[加了注释]
- JXTA的一个例子[加了注释]
- 一个搜索路径的例子程序 注释
- EhLib安装问题(dbsumlst.dcu出错)
- Ubuntu 12.04.5安装TL-WN725N 2.0无线网卡驱动
- Android Scroller简单用法
- sping 定时器 掉存过过程
- 如何从网页获取验证码并显示在窗口控件上呢?
- Qwt例子-CpuPlot注释版
- TextView 所有属性介绍
- Python 使用list实现简单的set
- Java基础----多态
- C# 处理事务
- 淘宝海量数据产品技术架构
- android 文件读取(assets)
- 1412202049-hpu-1004:C语言考试练习题_保留字母
- CentOS 6.6/Fedora 21 源码编译安装 mysql 5.6.22