ImageVerifierCode 换一换
格式:DOCX , 页数:42 ,大小:39.13KB ,
资源ID:7637396      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7637396.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(QT编程例子.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

QT编程例子.docx

1、QT编程例子第一个例子在这个例子里我们介绍了一个定时器来实现动画的射击。 t11/lcdrange.h包含LCDRange类定义。 t11/lcdrange.cpp包含LCDRange类实现。 t11/cannon.h包含CannonField类定义。 t11/cannon.cpp包含CannonField类实现。 t11/main.cpp包含MyWidget和main。 (1)t11/cannon.h CannonField现在就有了射击能力。 void shoot();当炮弹不在空中,调用这个槽会使加农炮射击。 private slots: void moveShot();当炮弹正在空中时

2、,这个私有槽使用一个定时器来移动射击。 private: void paintShot( QPainter * );这个函数来画射击。 QRect shotRect() const;当炮弹正在空中的时候,这个私有函数返回封装它所占用空间的矩形,否则它就返回一个没有定义的矩形。 int timerCount; QTimer * autoShootTimer; float shoot_ang; float shoot_f;这些私有变量包含了描述射击的信息。timerCount保留了射击进行后的时间。shoot_ang是加农炮射击时的角度,shoot_f是射击时加农炮的力量。 (2)t11/cann

3、on.cpp #include 我们包含了数学库,因为我们需要使用sin()和cos()函数。 CannonField:CannonField( QWidget *parent, const char *name ) : QWidget( parent, name ) ang = 45; f = 0; timerCount = 0; autoShootTimer = new QTimer( this, movement handler ); connect( autoShootTimer, SIGNAL(timeout(), this, SLOT(moveShot() ); shoot_ang

4、 = 0; shoot_f = 0; setPalette( QPalette( QColor( 250, 250, 200) ) ); 我们初始化我们新的私有变量并且把QTimer:timeout()信号和我们的moveShot()槽相连。我们会在定时器超时的时候移动射击。 void CannonField:shoot() if ( autoShootTimer-isActive() ) return; timerCount = 0; shoot_ang = ang; shoot_f = f; autoShootTimer-start( 50 );只要炮弹不在空中,这个函数就会进行一次射击。

5、timerCount被重新设置为零。shoot_ang和shoot_f设置为当前加农炮的角度和力量。最后,我们开始这个定时器。 void CannonField:moveShot() QRegion r( shotRect() ); timerCount+; QRect shotR = shotRect(); if ( shotR.x() width() | shotR.y() height() ) autoShootTimer-stop(); else r = r.unite( QRegion( shotR ) ); repaint( r ); moveShot()是一个移动射击的槽,当QT

6、imer开始的时候,每50毫秒被调用一次。它的任务就是计算新的位置,重新画屏幕并把炮弹放到新的位置,并且如果需要的话,停止定时器。首先我们使用QRegion来保留旧的shotRect()。QRegion可以保留任何种类的区域,并且我们可以用它来简化绘画过程。shotRect()返回现在炮弹所在的矩形稍后我们会详细介绍。 然后我们增加timerCount,用它来实现炮弹在它的轨迹中移动的每一步。 下一步我们算出新的炮弹的矩形。如果炮弹已经移动到窗口部件的右面或者下面的边界,我们停止定时器或者添加新的shotRect()到QRegion。 最后,我们重新绘制QRegion。这将会发送一个单一的绘画

7、事件,但仅仅有一个到两个举行需要刷新。 void CannonField:paintEvent( QPaintEvent *e ) QRect updateR = e-rect(); QPainter p( this ); if ( updateR.intersects( cannonRect() ) ) paintCannon( &p ); if ( autoShootTimer-isActive() & updateR.intersects( shotRect() ) ) paintShot( &p ); 绘画事件函数在前一章中已经被分成两部分了。现在我们得到的新的矩形区域需要绘画,检查加

8、农炮和/或炮弹是否相交,并且如果需要的话,调用paintCannon()和/或paintShot()。 void CannonField:paintShot( QPainter *p ) p-setBrush( black ); p-setPen( NoPen ); p-drawRect( shotRect() ); 这个私有函数画一个黑色填充的矩形作为炮弹。我们把paintCannon()的实现放到一边,它和前一章中的paintEvent()一样。 QRect CannonField:shotRect() const const double gravity = 4; double time

9、 = timerCount / 4.0; double velocity = shoot_f; double radians = shoot_ang*3.14159265/180; double velx = velocity*cos( radians ); double vely = velocity*sin( radians ); double x0 = ( barrelRect.right() + 5 )*cos(radians); double y0 = ( barrelRect.right() + 5 )*sin(radians); double x = x0 + velx*time

10、; double y = y0 + vely*time - 0.5*gravity*time*time; QRect r = QRect( 0, 0, 6, 6 ); r.moveCenter( QPoint( qRound(x), height() - 1 - qRound(y) ) ); return r; 这个私有函数计算炮弹的中心点并且返回封装炮弹的矩形。它除了使用自动增加所过去的时间的timerCount之外,还使用初始时的加农炮的力量和角度。 运算公式使用的是有重力的环境下光滑运动的经典牛顿公式。简单地说,我们已经选择忽略爱因斯坦理论的结果。 我们在一个y坐标向上增加的坐标系统中计

11、算中心点。在我们计算出中心点之后,我们构造一个6*6大小的QRect,并把它的中心移动到我们上面所计算出的中心点。同样的操作我们把这个点移动到窗口部件的坐标系统(请看坐标系统)。qRound()函数是一个在qglobal.h中定义的内嵌函数(被其它所有Qt头文件包含)。qRound()把一个双精度实数变为最接近的整数。 (3)t11/main.cpp class MyWidget: public QWidget public: MyWidget( QWidget *parent=0, const char *name=0 ); ;唯一的增加是Shoot按钮。 QPushButton *shoo

12、t = new QPushButton( &Shoot, this, shoot ); shoot-setFont( QFont( Times, 18, QFont:Bold ) );在构造函数中我们创建和设置Shoot按钮就像我们对Quit按钮所做的那样。注意构造函数的第一个参数是按钮的文本,并且第三个是窗口部件的名称。 connect( shoot, SIGNAL(clicked(), cannonField, SLOT(shoot() );把Shoot按钮的clicked()信号和CannonField的shoot()槽连接起来。 行为 The cannon can shoot, but

13、 theres nothing to shoot at. 第二个例子int QTimer:start ( intmsec, boolsshot = FALSE ) Analog ClockThis example displays an analog clock widget. #ifndef ACLOCK_H#define ACLOCK_H#include #include class AnalogClock : public QWidget / analog clock widget Q_OBJECTpublic: AnalogClock( QWidget *parent=0, const

14、 char *name=0 ); void setAutoMask(bool b);public slots: void setTime( const QTime & t );protected: void updateMask(); void paintEvent( QPaintEvent *); void mousePressEvent( QMouseEvent *); void mouseMoveEvent( QMouseEvent *);private slots: void drawClock( QPainter* ); void timeout();private: QPoint

15、clickPos; QTime time;#endif / ACLOCK_H#include aclock.h#include #include #include / Constructs an analog clock widget that uses an internal QTimer.AnalogClock:AnalogClock( QWidget *parent, const char *name ) : QWidget( parent, name ) time = QTime:currentTime(); / get current time QTimer *internalTim

16、er = new QTimer( this ); / create internal timer connect( internalTimer, SIGNAL(timeout(), SLOT(timeout() ); internalTimer-start( 5000 ); / emit signal every 5 secondsvoid AnalogClock:mousePressEvent( QMouseEvent *e ) if(isTopLevel() clickPos = e-pos() + QPoint(geometry().topLeft() - frameGeometry()

17、.topLeft();void AnalogClock:mouseMoveEvent( QMouseEvent *e ) if(isTopLevel() move( e-globalPos() - clickPos );void AnalogClock:setTime( const QTime & t ) time = t; timeout();/ The QTimer:timeout() signal is received by this slot.void AnalogClock:timeout() QTime new_time = QTime:currentTime(); / get

18、the current time time = time.addSecs( 5 ); if ( new_time.minute() != time.minute() ) / minute has changed if (autoMask() updateMask(); else update(); void AnalogClock:paintEvent( QPaintEvent * ) if ( autoMask() ) return; QPainter paint( this ); paint.setBrush( colorGroup().foreground() ); drawClock(

19、 &paint );/ If the clock is transparent, we use updateMask()/ instead of paintEvent()void AnalogClock:updateMask() / paint clock mask QBitmap bm( size() ); bm.fill( color0 ); /transparent QPainter paint; paint.begin( &bm, this ); paint.setBrush( color1 ); / use non-transparent color paint.setPen( co

20、lor1 ); drawClock( &paint ); paint.end(); setMask( bm );/ The clock is painted using a 1000x1000 square coordinate system, in the a centered square,/ as big as possible. The painters pen and brush colors are used.void AnalogClock:drawClock( QPainter *paint ) paint-save(); paint-setWindow( -500,-500,

21、 1000,1000 ); QRect v = paint-viewport(); int d = QMIN( v.width(), v.height() ); paint-setViewport( v.left() + (v.width()-d)/2, v.top() + (v.height()-d)/2, d, d ); / time = QTime:currentTime(); QPointArray pts; paint-save(); paint-rotate( 30*(time.hour()%12-3) + time.minute()/2 ); pts.setPoints( 4,

22、-20,0, 0,-20, 300,0, 0,20 ); paint-drawConvexPolygon( pts ); paint-restore(); paint-save(); paint-rotate( (time.minute()-15)*6 ); pts.setPoints( 4, -10,0, 0,-10, 400,0, 0,10 ); paint-drawConvexPolygon( pts ); paint-restore(); for ( int i=0; idrawLine( 440,0, 460,0 ); paint-rotate( 30 ); paint-restor

23、e();void AnalogClock:setAutoMask(bool b) if (b) setBackgroundMode( PaletteForeground ); else setBackgroundMode( PaletteBackground ); QWidget:setAutoMask(b);Main: #include aclock.h#include int main( int argc, char *argv ) QApplication a( argc, argv ); AnalogClock *clock = new AnalogClock; if ( argc =

24、 2 & strcmp( argv1, -transparent ) = 0 ) clock-setAutoMask( TRUE ); clock-resize( 100, 100 ); a.setMainWidget( clock ); clock-setCaption(Qt Example - Analog Clock); clock-show(); int result = a.exec(); delete clock; return result;See also Examples. A Directory BrowserThis example program demonstrate

25、s how to use a listview and listview items to build a multi-column hierarchical, memory- and CPU-efficient directory browser. It also demonstrates how to use Drag&Drop in a listview. Header file: #ifndef DIRVIEW_H#define DIRVIEW_H#include #include #include #include #include class QWidget;class QDrag

26、EnterEvent;class QDragMoveEvent;class QDragLeaveEvent;class QDropEvent;class FileItem : public QListViewItempublic: FileItem( QListViewItem *parent, const QString &s1, const QString &s2 ) : QListViewItem( parent, s1, s2 ), pix( 0 ) const QPixmap *pixmap( int i ) const; void setPixmap( QPixmap *p );p

27、rivate: QPixmap *pix;class Directory : public QListViewItempublic: Directory( QListView * parent, const QString& filename ); Directory( Directory * parent, const QString& filename, const QString &col2 ) : QListViewItem( parent, filename, col2 ), pix( 0 ) Directory( Directory * parent, const QString&

28、 filename ); QString text( int column ) const; QString fullName(); void setOpen( bool ); void setup(); const QPixmap *pixmap( int i ) const; void setPixmap( QPixmap *p );private: QFile f; Directory * p; bool readable; bool showDirsOnly; QPixmap *pix;class DirectoryView : public QListView Q_OBJECTpub

29、lic: DirectoryView( QWidget *parent = 0, const char *name = 0, bool sdo = FALSE ); bool showDirsOnly() return dirsOnly; public slots: void setDir( const QString & );signals: void folderSelected( const QString & );protected slots: void slotFolderSelected( QListViewItem * ); void openFolder();protected: void contentsDragEnterEvent( QDragEnterEvent *e ); void contentsDragMoveEvent( QDragMoveEvent *e ); void contentsDragLeaveEvent( QDragLeaveEvent *e ); void contentsDropEvent( QDropEvent *e ); void contentsMouseMoveEvent( QMouseEvent *e ); void contentsMousePressEvent( QMous

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1