1、QT编写动态曲线喜马拉雅-巅峰之作主页博客相册|个人档案 |好友 |i贴吧 查看文章【转】 qt实现实时动态曲线(一)2011年04月19日 星期二 下午 12:39转载自 Linuxzrq最终编辑 Linuxzrqdisplay.h:#ifndef DISPLAY_H#define DISPLAY_H#ifndef QT_H#include #endif / QT_Hclass QTimer;class Screen;class QStringList;class QString;class QLineEdit;class QPushButton;class DisplayWidget :
2、public QWidget Q_OBJECTpublic: DisplayWidget( QWidget *parent=0, const char *name=0 ); QSize sizeHint() const; double readCurveDate(); void readFile(); void run();private slots: void tick(); void start();/startbutton void stop();/stopbuttonprivate: Screen *screen1; QLineEdit *lineEdit;/display curve
3、data QPushButton *startButton, *stopButton; QTimer *timer; enum Margin = 40; QString str; QStringList strlist; QStringList :Iterator it; int time; double yval;#endif / PLOT_Hdisplay.cpp:#include display.h#include screen.h#include #include #include #include #include #include #include #include #includ
4、e #include #include #include using namespace std;DisplayWidget:DisplayWidget( QWidget *parent, const char *name ) : QWidget( parent, name ) timer = 0; QVBoxLayout *vbox = new QVBoxLayout( this, 10 ); QHBoxLayout *hbox = new QHBoxLayout( vbox ); screen1 = new Screen( this ); screen1-setYTitle( QObjec
5、t:tr( dynamic curve ) ); vbox-addWidget( screen1 ); lineEdit = new QLineEdit(this); lineEdit-setReadOnly( TRUE ); hbox-addWidget( lineEdit ); startButton = new QPushButton( this ); startButton-setText( tr( &Start ) ); startButton-setAccel( QKeySequence( tr( Alt+S ) ) ); stopButton = new QPushButton(
6、 this ); stopButton-setText( tr( Sto&p ) ); stopButton-setAccel( QKeySequence( tr( Alt+P ) ) ); hbox-addWidget( startButton ); hbox-addWidget( stopButton ); connect( startButton, SIGNAL( clicked () ), SLOT( start() ) ); connect( stopButton, SIGNAL( clicked () ), SLOT( stop() ) ); time = 0; yval = 0.
7、0; readFile();void DisplayWidget:run() if ( !timer ) timer = new QTimer( this ); connect( timer, SIGNAL( timeout() ), SLOT( tick() ) ); timer-start( 1000 );/delay 1svoid DisplayWidget:tick() yval = readCurveDate(); screen1-animate( yval ); lineEdit-setText( QString:number( yval ) );/display curvedat
8、avoid DisplayWidget:start() run();void DisplayWidget:stop() timer-stop();QSize DisplayWidget:sizeHint() const return QSize( 32 * Margin, 24 * Margin );/read data from filevoid DisplayWidget:readFile() QFile file(in.txt); file.open(QIODevice:ReadOnly); QTextStream in(&file); str = in.read(); strlist
9、= QStringList:split( , str); it = strlist.begin(); double DisplayWidget:readCurveDate( ) QString tempStr; double tempDate; tempStr =(QString) *it; tempDate = tempStr.toDouble(); if (it != strlist.end() +it; else it = strlist.begin(); return tempDate;screen.h:#ifndef SCREEN_H#define SCREEN_H#include
10、#include #include #include #include #include #define FrameWidth 2#define Step 5 #define BaseFontHeight 16#define BaseLineLenght 5 #define SpaceMargin 4class QRect;class QString;class Screen : public QFrame Q_OBJECTpublic: Screen( QWidget *parent=0, const char *name=0, Qt:WFlags flags=0 ); void anima
11、te( double y); void setXTitle( QString str ); void setYTitle( QString str );protected: void initNumber( void ); void initCordinate( QPainter &p ); void updateCurve( QPainter &p ); virtual void showEvent ( QShowEvent * ); virtual void hideEvent ( QHideEvent * ); QSize minimumSize () const;public: dou
12、ble newY, oldY; int numXTicks, numYTicks; QValueVector Yval; bool firstShow; int sec, min, hour; QPixmap saveBuffer, newBuffer, midBuffer; QRect rectCordinate; QRect fromSaveRect; QRect toNewRect; QRect rectYText; QRect rectXText; /*We use this painter to draw evering on the newbuffer.*/ QPainter dr
13、awPainter; QString stringYTitle; QString stringXTitle;#endif /*SCREEN_H*/screen.cpp:#include screen.h#include #include #include #include #include #include #include #include #include #include #include Screen:Screen( QWidget *parent, const char *name, Qt:WFlags flags ): QFrame( parent, name, flags | Q
14、t:WNoAutoErase ) setLineWidth( FrameWidth ); setFrameStyle( Panel | Sunken ); setBackgroundMode( Qt:PaletteBase ); setSizePolicy( QSizePolicy:MinimumExpanding, QSizePolicy:MinimumExpanding ); firstShow = TRUE;void Screen:showEvent( QShowEvent * ) if ( firstShow ) initNumber(); initCordinate( drawPai
15、nter );QSize Screen:minimumSize () const return QSize( 20 * SpaceMargin, 20 * SpaceMargin );void Screen:hideEvent( QHideEvent * ) firstShow = FALSE;void Screen:setXTitle(QString str ) stringXTitle = str;void Screen:setYTitle( QString str ) stringYTitle = str;void Screen:initNumber( ) saveBuffer.resi
16、ze( size() ); /set the size and the backcolor of saveBuffer.fill( this, 0, 0 );/those three QPixmaps as same as the Screen newBuffer.resize( size() ); newBuffer.fill( this, 0, 0 ); midBuffer.resize( size() ); midBuffer.fill( this, 0, 0 ); drawPainter.begin(&newBuffer); QRect newWindow = drawPainter.
17、window(); newY = 0; oldY =0; sec = 0; min = 0; hour = 0; stringXTitle = QObject:tr( Time (hh:mm:ss) ); Yval.push_back( (double)oldY ); rectCordinate.setRect( newWindow.topLeft().x()+FrameWidth + 2 * BaseFontHeight + 2 * BaseLineLenght, newWindow.topLeft().y()+FrameWidth + 2 * SpaceMargin, newWindow.
18、width() - 2 * ( FrameWidth + BaseFontHeight + BaseLineLenght + SpaceMargin ), newWindow.height() - 2 * ( FrameWidth + BaseFontHeight +BaseLineLenght+SpaceMargin ) ); if ( 0 != ( rectCordinate.width() % (Step*Step) ) ) int x = rectCordinate.width() % ( Step * Step ); /( (int) sqrt( (float) Step ) );
19、rectCordinate.setWidth( rectCordinate.width() - x+1 ); if ( 0 != ( rectCordinate.height() % (Step*Step) ) ) int y = rectCordinate.height() % (Step *Step ); rectCordinate.setHeight( rectCordinate.height() - y+1 ); numXTicks = rectCordinate.width() / Step; numYTicks = rectCordinate.height() / Step; re
20、ctYText.setRect(newWindow.topLeft().x() + FrameWidth,newWindow.topLeft().y(+FrameWidth+2 * SpaceMargin,BaseFontHeight, rectCordinate.height() ); rectXText.setRect(rectCordinate.bottomLeft().x(), newWindow.bottomLeft().y() - FrameWidth - BaseFontHeight,rectCordinate.width(), BaseFontHeight ); fromSav
21、eRect.setRect(rectCordinate.topLeft().x() + Step,rectCordinate.topLeft().y() + 1, rectCordinate.width() - Step - 1, rectCordinate.height() + 2 * BaseLineLenght + BaseFontHeight ); toNewRect.setRect(rectCordinate.topLeft().x() + 1,rectCordinate.topLeft().y() + 1, rectCordinate.width() - Step - 1, rec
22、tCordinate.height() + 2 * BaseLineLenght + BaseFontHeight ); drawPainter.drawRect(toNewRect);void Screen:initCordinate( QPainter &pCordinate )if ( firstShow ) pCordinate.setPen( Qt:blue ); pCordinate.drawRect( rectCordinate ); this-setAttribute(Qt:WA_PaintOutsidePaintEvent);/ int y0 = rectCordinate.
23、bottomLeft().y(); int x0 = rectCordinate.bottomLeft().x(); int yText = 0; for (int j = 0; j = numYTicks; j + ) pCordinate.drawLine( x0 - BaseLineLenght, y0, x0, y0 ); if (0 = j % Step ) pCordinate.drawLine( x0 - 2 * BaseLineLenght, y0, x0 - BaseLineLenght, y0 ); pCordinate.save(); pCordinate.setPen(
24、 QPen( Qt:blue, 1, Qt:DotLine) ); pCordinate.drawLine( x0 , y0, rectCordinate.bottomRight().x(), y0 ); pCordinate.restore(); pCordinate.setPen( Qt:black ); pCordinate.drawText( x0 - 2 * BaseLineLenght - BaseFontHeight, y0 - 2 * BaseFontHeight + 5 * Step,BaseFontHeight, BaseFontHeight + Step, Qt:AlignCenter , QString:number( yText) ); yText +=10; pCordinate.setPen( Qt:blue );
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1