QT编程例子.docx
《QT编程例子.docx》由会员分享,可在线阅读,更多相关《QT编程例子.docx(42页珍藏版)》请在冰豆网上搜索。
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现在就有了射击能力。
voidshoot();
当炮弹不在空中,调用这个槽会使加农炮射击。
privateslots:
voidmoveShot();
当炮弹正在空中时,这个私有槽使用一个定时器来移动射击。
private:
voidpaintShot(QPainter*);
这个函数来画射击。
QRectshotRect()const;
当炮弹正在空中的时候,这个私有函数返回封装它所占用空间的矩形,否则它就返回一个没有定义的矩形。
inttimerCount;
QTimer*autoShootTimer;
floatshoot_ang;
floatshoot_f;
这些私有变量包含了描述射击的信息。
timerCount保留了射击进行后的时间。
shoot_ang是加农炮射击时的角度,shoot_f是射击时加农炮的力量。
(2)t11/cannon.cpp
#include
我们包含了数学库,因为我们需要使用sin()和cos()函数。
CannonField:
:
CannonField(QWidget*parent,constchar*name)
:
QWidget(parent,name)
{
ang=45;
f=0;
timerCount=0;
autoShootTimer=newQTimer(this,"movementhandler");
connect(autoShootTimer,SIGNAL(timeout()),this,SLOT(moveShot()));
shoot_ang=0;
shoot_f=0;
setPalette(QPalette(QColor(250,250,200)));
}
我们初始化我们新的私有变量并且把QTimer:
:
timeout()信号和我们的moveShot()槽相连。
我们会在定时器超时的时候移动射击。
voidCannonField:
:
shoot()
{
if(autoShootTimer->isActive())
return;
timerCount=0;
shoot_ang=ang;
shoot_f=f;
autoShootTimer->start(50);
}
只要炮弹不在空中,这个函数就会进行一次射击。
timerCount被重新设置为零。
shoot_ang和shoot_f设置为当前加农炮的角度和力量。
最后,我们开始这个定时器。
voidCannonField:
:
moveShot()
{
QRegionr(shotRect());
timerCount++;
QRectshotR=shotRect();
if(shotR.x()>width()||shotR.y()>height())
autoShootTimer->stop();
else
r=r.unite(QRegion(shotR));
repaint(r);
}
moveShot()是一个移动射击的槽,当QTimer开始的时候,每50毫秒被调用一次。
它的任务就是计算新的位置,重新画屏幕并把炮弹放到新的位置,并且如果需要的话,停止定时器。
首先我们使用QRegion来保留旧的shotRect()。
QRegion可以保留任何种类的区域,并且我们可以用它来简化绘画过程。
shotRect()返回现在炮弹所在的矩形——稍后我们会详细介绍。
然后我们增加timerCount,用它来实现炮弹在它的轨迹中移动的每一步。
下一步我们算出新的炮弹的矩形。
如果炮弹已经移动到窗口部件的右面或者下面的边界,我们停止定时器或者添加新的shotRect()到QRegion。
最后,我们重新绘制QRegion。
这将会发送一个单一的绘画事件,但仅仅有一个到两个举行需要刷新。
voidCannonField:
:
paintEvent(QPaintEvent*e)
{
QRectupdateR=e->rect();
QPainterp(this);
if(updateR.intersects(cannonRect()))
paintCannon(&p);
if(autoShootTimer->isActive()&&updateR.intersects(shotRect()))
paintShot(&p);
}
绘画事件函数在前一章中已经被分成两部分了。
现在我们得到的新的矩形区域需要绘画,检查加农炮和/或炮弹是否相交,并且如果需要的话,调用paintCannon()和/或paintShot()。
voidCannonField:
:
paintShot(QPainter*p)
{
p->setBrush(black);
p->setPen(NoPen);
p->drawRect(shotRect());
}
这个私有函数画一个黑色填充的矩形作为炮弹。
我们把paintCannon()的实现放到一边,它和前一章中的paintEvent()一样。
QRectCannonField:
:
shotRect()const
{
constdoublegravity=4;
doubletime=timerCount/4.0;
doublevelocity=shoot_f;
doubleradians=shoot_ang*3.14159265/180;
doublevelx=velocity*cos(radians);
doublevely=velocity*sin(radians);
doublex0=(barrelRect.right()+5)*cos(radians);
doubley0=(barrelRect.right()+5)*sin(radians);
doublex=x0+velx*time;
doubley=y0+vely*time-0.5*gravity*time*time;
QRectr=QRect(0,0,6,6);
r.moveCenter(QPoint(qRound(x),height()-1-qRound(y)));
returnr;
}
这个私有函数计算炮弹的中心点并且返回封装炮弹的矩形。
它除了使用自动增加所过去的时间的timerCount之外,还使用初始时的加农炮的力量和角度。
运算公式使用的是有重力的环境下光滑运动的经典牛顿公式。
简单地说,我们已经选择忽略爱因斯坦理论的结果。
我们在一个y坐标向上增加的坐标系统中计算中心点。
在我们计算出中心点之后,我们构造一个6*6大小的QRect,并把它的中心移动到我们上面所计算出的中心点。
同样的操作我们把这个点移动到窗口部件的坐标系统(请看坐标系统)。
qRound()函数是一个在qglobal.h中定义的内嵌函数(被其它所有Qt头文件包含)。
qRound()把一个双精度实数变为最接近的整数。
(3)t11/main.cpp
classMyWidget:
publicQWidget
{
public:
MyWidget(QWidget*parent=0,constchar*name=0);
};
唯一的增加是Shoot按钮。
QPushButton*shoot=newQPushButton("&Shoot",this,"shoot");
shoot->setFont(QFont("Times",18,QFont:
:
Bold));
在构造函数中我们创建和设置Shoot按钮就像我们对Quit按钮所做的那样。
注意构造函数的第一个参数是按钮的文本,并且第三个是窗口部件的名称。
connect(shoot,SIGNAL(clicked()),cannonField,SLOT(shoot()));
把Shoot按钮的clicked()信号和CannonField的shoot()槽连接起来。
行为
Thecannoncanshoot,butthere'snothingtoshootat.
第二个例子
intQTimer:
:
start(int msec,bool sshot=FALSE)
AnalogClock
Thisexampledisplaysananalogclockwidget.
#ifndefACLOCK_H
#defineACLOCK_H
#include
#include
classAnalogClock:
publicQWidget//analogclockwidget
{
Q_OBJECT
public:
AnalogClock(QWidget*parent=0,constchar*name=0);
voidsetAutoMask(boolb);
publicslots:
voidsetTime(constQTime&t);
protected:
voidupdateMask();
voidpaintEvent(QPaintEvent*);
voidmousePressEvent(QMouseEvent*);
voidmouseMoveEvent(QMouseEvent*);
privateslots:
voiddrawClock(QPainter*);
voidtimeout();
private:
QPointclickPos;
QTimetime;
};
#endif//ACLOCK_H
#include"aclock.h"
#include
#include
#include
//ConstructsananalogclockwidgetthatusesaninternalQTimer.
AnalogClock:
:
AnalogClock(QWidget*parent,constchar*name)
:
QWidget(parent,name)
{
time=QTime:
:
currentTime();//getcurrenttime
QTimer*internalTimer=newQTimer(this);//createinternaltimer
connect(internalTimer,SIGNAL(timeout()),SLOT(timeout()));
internalTimer->start(5000);//emitsignalevery5seconds
}
voidAnalogClock:
:
mousePressEvent(QMouseEvent*e)
{
if(isTopLevel())
clickPos=e->pos()+QPoint(geometry().topLeft()-frameGeometry().topLeft());
}
voidAnalogClock:
:
mouseMoveEvent(QMouseEvent*e)
{
if(isTopLevel())
move(e->globalPos()-clickPos);
}
voidAnalogClock:
:
setTime(constQTime&t)
{
time=t;
timeout();
}
//TheQTimer:
:
timeout()signalisreceivedbythisslot.
voidAnalogClock:
:
timeout()
{
QTimenew_time=QTime:
:
currentTime();//getthecurrenttime
time=time.addSecs(5);
if(new_time.minute()!
=time.minute()){//minutehaschanged
if(autoMask())updateMask();
elseupdate();
}
}
voidAnalogClock:
:
paintEvent(QPaintEvent*)
{
if(autoMask())return;
QPainterpaint(this);
paint.setBrush(colorGroup().foreground());
drawClock(&paint);
}
//Iftheclockistransparent,weuseupdateMask()
//insteadofpaintEvent()
voidAnalogClock:
:
updateMask()//paintclockmask
{
QBitmapbm(size());
bm.fill(color0);//transparent
QPainterpaint;
paint.begin(&bm,this);
paint.setBrush(color1);//usenon-transparentcolor
paint.setPen(color1);
drawClock(&paint);
paint.end();
setMask(bm);
}
//Theclockispaintedusinga1000x1000squarecoordinatesystem,intheacenteredsquare,
//asbigaspossible.Thepainter'spenandbrushcolorsareused.
voidAnalogClock:
:
drawClock(QPainter*paint)
{
paint->save();
paint->setWindow(-500,-500,1000,1000);
QRectv=paint->viewport();
intd=QMIN(v.width(),v.height());
paint->setViewport(v.left()+(v.width()-d)/2,v.top()+(v.height()-d)/2,d,d);
//time=QTime:
:
currentTime();
QPointArraypts;
paint->save();
paint->rotate(30*(time.hour()%12-3)+time.minute()/2);
pts.setPoints(4,-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(inti=0;i<12;i++){
paint->drawLine(440,0,460,0);
paint->rotate(30);
}
paint->restore();
}
voidAnalogClock:
:
setAutoMask(boolb)
{
if(b)setBackgroundMode(PaletteForeground);
elsesetBackgroundMode(PaletteBackground);
QWidget:
:
setAutoMask(b);
}
Main:
#include"aclock.h"
#include
intmain(intargc,char**argv)
{
QApplicationa(argc,argv);
AnalogClock*clock=newAnalogClock;
if(argc==2&&strcmp(argv[1],"-transparent")==0)
clock->setAutoMask(TRUE);
clock->resize(100,100);
a.setMainWidget(clock);
clock->setCaption("QtExample-AnalogClock");
clock->show();
intresult=a.exec();
deleteclock;
returnresult;
}
SeealsoExamples.
ADirectoryBrowser
Thisexampleprogramdemonstrateshowtousealistviewandlistviewitemstobuildamulti-columnhierarchical,memory-andCPU-efficientdirectorybrowser.ItalsodemonstrateshowtouseDrag&Dropinalistview.
Headerfile:
#ifndefDIRVIEW_H
#defineDIRVIEW_H
#include
#include
#include
#include
#include
classQWidget;
classQDragEnterEvent;
classQDragMoveEvent;
classQDragLeaveEvent;
classQDropEvent;
classFileItem:
publicQListViewItem
{
public:
FileItem(QListViewItem*parent,constQString&s1,constQString&s2)
:
QListViewItem(parent,s1,s2),pix(0){}
constQPixmap*pixmap(inti)const;
voidsetPixmap(QPixmap*p);
private:
QPixmap*pix;
};
classDirectory:
publicQListViewItem
{
public:
Directory(QListView*parent,constQString&filename);
Directory(Directory*parent,constQString&filename,constQString&col2)
:
QListViewItem(parent,filename,col2),pix(0){}
Directory(Directory*parent,constQString&filename);
QStringtext(intcolumn)const;
QStringfullName();
voidsetOpen(bool);
voidsetup();
constQPixmap*pixmap(inti)const;
voidsetPixmap(QPixmap*p);
private:
QFilef;
Directory*p;
boolreadable;
boolshowDirsOnly;
QPixmap*pix;
};
classDirectoryView:
publicQListView
{
Q_OBJECT
public:
DirectoryView(QWidget*parent=0,constchar*name=0,boolsdo=FALSE);
boolshowDirsOnly(){returndirsOnly;}
publicslots:
voidsetDir(constQString&);
signals:
voidfolderSelected(constQString&);
protectedslots:
voidslotFolderSelected(QListViewItem*);
voidopenFolder();
protected:
voidcontentsDragEnterEvent(QDragEnterEvent*e);
voidcontentsDragMoveEvent(QDragMoveEvent*e);
voidcontentsDragLeaveEvent(QDragLeaveEvent*e);
voidcontentsDropEvent(QDropEvent*e);
voidcontentsMouseMoveEvent(QMouseEvent*e);
voidcontentsMousePressEvent(QMous