实训四的资料_第1页
实训四的资料_第2页
实训四的资料_第3页
实训四的资料_第4页
实训四的资料_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

《嵌入式操作系统课程设计》实训报告PAGE第1页共23页《嵌入式操作系统B》实训报告上海第二工业大学计算机科学与技术系学生实训报告课程名称嵌入式操作系统课程设计实训类别验证型实训项目名称布局管理、事件处理和图像班级10计科A1姓名学号实训时间2013年11月1日实训地点15号楼507指导教师组号同组学生信息(请填写在下方)班级姓名学号一、实训目的1、了解并掌握QT提供的多种布局管理部件,包括QT布局管理器、分裂器、栈部件、工作空间部件和多文档区部件等;2、分析Qt的常用事件,包括鼠标事件、键盘事件以及事件过滤器的使用方法;3、掌握QPainter、QCanvas与OpenGL进行绘图的基本方法,并注意这三类方式使用的异同点。二、设备和仪器装有Linux和Windows操作系统的PC机一台实训内容完成书本第六部分“布局管理”中的两个例题QScrollView的IconEditor和多文档界面的Editor;QScrollView的IconEditor运行结果:主要代码注释如下:ImageEditor.cppImageEditor::ImageEditor(QWidget*parent,constchar*name):QScrollView(parent,name,WStaticContents|WNoAutoErase){curColor=black;zoom=8;curImage.create(16,16,32);//若没有加载图片,则将画布的大小设置为16*16大小的curImage.fill(qRgba(0,0,0,0));//颜色为白色curImage.setAlphaBuffer(true);resizeContents();//此函数用于管理初始窗口部件获取自己的大小}voidImageEditor::resizeContents(){QSizesize=zoom*curImage.size();if(zoom>=3)size+=QSize(1,1);//若zoom大于等于3,size则放大一格QScrollView::resizeContents(size.width(),size.height());//QScrollView是否显示滚动条由窗口的大小决定}About菜单为:AboutQt菜单为实验过程:运行结果:完成书本第七部分“事件处理”中的例题Ticker;#include<qpainter.h>#include"ticker.h"Ticker::Ticker(QWidget*parent,constchar*name):QWidget(parent,name){offset=0;//设置被绘制文本X坐标myTimerId=0;//定时器标示器}voidTicker::setText(constQString&newText){myText=newText;update();//强制重新绘制updateGeometry();//提示ticker窗口布局的变化}QSizeTicker::sizeHint()const{returnfontMetrics().size(0,text());}voidTicker::paintEvent(QPaintEvent*){QPainterpainter(this);inttextWidth=fontMetrics().width(text());//确定文版需要多少水平空间if(textWidth<1)return;intx=-offset;while(x<width()){painter.drawText(x,0,textWidth,height(),AlignLeft|AlignVCenter,text());x+=textWidth;}}voidTicker::showEvent(QShowEvent*){myTimerId=startTimer(30);//设置一个30秒的定时器}voidTicker::timerEvent(QTimerEvent*event)//时间间隔调用{if(event->timerId()==myTimerId){++offset;if(offset>=fontMetrics().width(text()))offset=0;scroll(-1,0); //左移一个像素}else{QWidget::timerEvent(event);}}voidTicker::hideEvent(QHideEvent*){killTimer(myTimerId);//当文本消失时,释放对应的定时器}请按下列程序源代码,编写ch_601,并在程序后进行注释:ch_601.cpp的源程序为:#include<qtimer.h>#include<qpainter.h>#include<qapplication.h>#include<qtextcodec.h>#include<stdlib.h>#include"ch_601.h"ch_601::ch_601(QWidget*parent,constchar*name):QWidget(parent,name){for(inta=0;a<numColors;a++)//声明变量a产生随机颜色{colors[a]=QColor(rand()&255,rand()&255);//设置随机颜色}rectangles=0;//矩形计数为0startTimer(0);//开始计数为0QTimer*counter=newQTimer(this);connect(counter,SIGNAL(timeout()),this,SLOT(updateCaption()));//计数器的信号为timeout时,更新captioncounter->start(1000);}voidch_601::updateCaption(){QStrings;s.sprintf(QObject::tr("QTimer实例"),rectangles);//设置caption为“QTimer实例”和举行数rectangles=0;setCaption(s);}voidch_601::paintEvent(QPaintEvent*){QPainterpaint(this);intw=width();//宽winth=height();//高hif(w<=0||h<=0)//若w<=0或h<=0return;//返回paint.setPen(NoPen);//若没有画笔则设置画笔paint.setBrush(colors[rand()%numColors]);//画刷颜色随机QPointp1(rand()%w,rand()%h);//设置矩形的位置QPointp2(rand()%w,rand()%h);QRectr(p1,p2);paint.drawRect(r);//画矩形}voidch_601::timerEvent(QTimerEvent*){for(inti=0;i<100;i++){repaint(false);//在一个绘画窗口发送之前,之前的窗口不会被擦除rectangles++;//矩形累加}}Main.cpp的源程序为:#include<qapplication.h>#include<qtextcodec.h>#include"ch_601.h"intmain(intargc,char**argv){QApplicationa(argc,argv);ch_601always(0,0);QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));//设置标题字体always.resize(400,250); //初始化窗体a.setMainWidget(&always);always.setCaption(QObject::tr("时钟的测试例题"));always.show();returna.exec();}ch_601.h的源程序为:#ifndefCH_601_H#defineCH_601_H#include<qwidget.h>constintnumColors=120;classCh_601:publicQWidget{Q_OBJECTpublic:Ch_601(QWidget*parent=0,constchar*name=0);protected:void paintEvent(QPaintEvent*);void timerEvent(QTimerEvent*);privateslots:void updateCaption();private:int rectangles;QColor colors[numColors];};#endifCh_601运行结果:请按下列程序源代码(其中主程序自编),编写ch_602,并在程序后进行注释:#include"progressbar.h"#include<qradiobutton.h>#include<qpushbutton.h>#include<qprogressbar.h>#include<qlayout.h>#include<qmotifstyle.h>ProgressBar::ProgressBar(QWidget*parent,constchar*name):QButtonGroup(0,Horizontal,QObject::tr("进度条测试"),parent,name),timer(){setMargin(10);//设置边距QGridLayout*toplayout=newQGridLayout(layout(),2,2,5);//设置空间组setRadioButtonExclusive(TRUE);//按键单一选择slow=newQRadioButton(QObject::tr("低速"),this);//设置低速,中速,高速三个选项normal=newQRadioButton(QObject::tr("中速"),this);fast=newQRadioButton(QObject::tr("高速"),this);QVBoxLayout*vb1=newQVBoxLayout;//新建一个boxtoplayout->addLayout(vb1,0,0); //嵌套布局vb1->addWidget(slow); //添加slow,normal,fast到布局vb1->addWidget(normal);vb1->addWidget(fast);start=newQPushButton(QObject::tr("开始"),this);//新建开始按钮reset=newQPushButton(QObject::tr("重启"),this);//新建重启按钮QVBoxLayout*vb2=newQVBoxLayout;toplayout->addLayout(vb2,0,1);vb2->addWidget(start);vb2->addWidget(reset);progress=newQProgressBar(100,this);//添加进度条toplayout->addMultiCellWidget(progress,1,1,0,1);//1列插入progressbar占2行2列connect(start,SIGNAL(clicked()),this,SLOT(slotStart()));//点击开始触发开始槽connect(reset,SIGNAL(timeout()),this,SLOT(slotTimeout()));//点击重置触发timeout槽normal->setChecked(TRUE);//默认选中设置为中速start->setFixedWidth(80);//设置最大最小宽度为80setMinimumWidth(300);//设置最小宽度300}voidProgressBar::slotStart(){if(progress->progress()==-1){if(slow->isChecked())progress->setTotalSteps(10000);//slow进度条满需10000小时elseif(normal->isChecked())progress->setTotalSteps(1000);//normal进度条满需1000小时elseprogress->setTotalSteps(50);//fast进度条满需50小时slow->setEnabled(FALSE);//slow不能选中normal->setEnabled(FALSE);//normal不能选中fast->setEnabled(FALSE); /fast不能选中}if(!timer.isActive())//若时钟未被激活{timer.start(1);//时钟设置为1start->setText(QObject::tr("暂停"));}else{timer.stop();//时间停止start->setText(QObject::tr("继续"));}}voidProgressBar::slotReset()//进度条重置{timer.stop();start->setText(QObject::tr("开始"));start->setEnabled(TRUE);//start可以被选择slow->setEnabled(TRUE);//slow可以被选择normal->setEnabled(TRUE);//normal可以被选择fast->setEnabled(TRUE);//fast可以被选择progress->reset();//进度条重置}voidProgressBar::slotTimeout(){intp=progress->progress();//声明p,用于保存进度条#if1if(p==progress->totalSteps())//若进度条满{start->setText(QObject::tr("开始"));start->setEnabled(FALSE);return;}#endifprogress->setProgress(++p);}processbar.h的源程序为:#ifndefPROGRESSBAR_H#definePROGRESSBAR_H#include<qbuttongroup.h>#include<qtimer.h>classQRadioButton;classQPushButton;classQProgressBar;classProgressBar:publicQButtonGroup{Q_OBJECTpublic:ProgressBar(QWidget*parent=0,constchar*name=0);protected:QRadioButton*slow,*normal,*fast;QPushButton*start,*pause,*reset;QProgressBar*progress;QTimertimer;protectedslots:voidslotStart();voidslotReset();voidslotTimeout();};#endifMain.cpp源程序为:#include<qapplication.h>#include"progressbar.h"intmain(intargc,char**argv){QApplicationa(argc,argv);ProgressBar*progrssbar=newProgressBar;a.setMainWidget(progrssbar);progrssbar->show();//显示窗体returna.exec();}运行界面如下:请按下列程序源代码,编写ch_801,并在程序后进行注释。ch_801.cpp的源程序为:#include<qapplication.h>#include<qpainter.h>#include<qpicture.h>#include<qpixmap.h>#include<qwidget.h>#include<qmessagebox.h>#include<qfile.h>#include<ctype.h>#include<qtextcodec.h>voidpaintCar(QPainter*p) {QPointArraya;QBrushbrush(Qt::green,Qt::SolidPattern);//设置刷子为绿色,并且是满填充p->setBrush(brush); //选中刷子a.setPoints(4,50,150,650,150,650,350,50,350);//设置点p->drawPolygon(a); //点内画一个多边形QFontf("courier",20,QFont::Bold);p->setFont(f);//设置字体QColorwindowColor(120,120,255); //设置窗体颜色brush.setColor(windowColor); //刷子设置为窗体颜色p->setBrush(brush); p->drawRect(100,180,100,70); //画4个窗体 p->drawRect(220,180,100,70); p->drawRect(340,180,100,70); p->drawRect(460,180,100,70);p->drawText(50,250,650,70,Qt::AlignCenter,QObject::tr("公共汽车"));//车体上显示的字 p->setBrush(Qt::red); //刷子为红色 p->drawRect(600,110,20,40);// p->setBrush(Qt::yellow); p->drawEllipse(610,70,20,20); p->drawEllipse(640,35,30,30); p->drawEllipse(670,0,40,40);p->setBackgroundMode(Qt::OpaqueMode); //轮胎背景不透明p->setBrush(Qt::DiagCrossPattern); p->drawEllipse(90,310,80,80); p->setBrush(Qt::DiagCrossPattern); p->drawEllipse(310,310,80,80); p->setBrush(Qt::DiagCrossPattern); p->drawEllipse(530,310,80,80);}classPictureDisplay:publicQWidget {public:PictureDisplay(constchar*fileName); //图片显示~PictureDisplay();protected:void paintEvent(QPaintEvent*);void keyPressEvent(QKeyEvent*);private:QPicture*pict;QString name;};PictureDisplay::PictureDisplay(constchar*fileName){pict=newQPicture;name=fileName;if(!pict->load(fileName)){ //若没有符合名字的图片 deletepict; pict=0; name.sprintf("Notabletoloadpicture:%s",fileName);}}PictureDisplay::~PictureDisplay(){deletepict;}voidPictureDisplay::paintEvent(QPaintEvent*){QPainterpaint(this); if(pict) paint.drawPicture(*pict); //画图else paint.drawText(rect(),AlignCenter,name);//写字}voidPictureDisplay::keyPressEvent(QKeyEvent*k){switch(tolower(k->ascii())){//判断主程序显示图片按键按下去的情况 case'r': //按下r pict->load(name);//显示name update(); //更新 break; case'q': //按下q QApplication::exit();//退出应用程序 break;}}intmain(intargc,char**argv){QApplicationa(argc,argv); QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));constchar*fileName="car.pic"; //设置fileName if(argc==2) fileName=argv[1];if(!QFile::exists(fileName)){//如果fileName图片不存在 QPicturepict; QPainterpaint; paint.begin(&pict); paintCar(&paint); paint.end(); pict.save(fileName); //保存fileName QMessageBox::information(0,"BUSEXAMPLE",QObject::tr("关闭对话框后请重新运行我,图片就出来了!")); return0;}else{ PictureDisplaytest(fileName); a.setMainWidget(&test); test.setCaption("BUSEXAMPLE"); test.show(); //显示 returna.exec(); }}完成书本P:211“使用OpenGL进行绘图”中的例题Cube;#include<qcolordialog.h>#include"cube.h"Cube::Cube(QWidget*parent,constchar*name):QGLWidget(parent,name){setFormat(QGLFormat(DoubleBuffer|DepthBuffer));//定义各式双缓存|深度缓存rotationX=0; //初始化3条轴rotationY=0;rotationZ=0;faceColors[0]=red; //定义六个面的颜色分别为红色,绿色,蓝色,粉色,黄色,草绿色faceColors[1]=green;faceColors[2]=blue;faceColors[3]=cyan;faceColors[4]=yellow;faceColors[5]=magenta;}voidCube::initializeGL(){qglClearColor(black);//用黑色初始化glShadeModel(GL_FLAT);//阴影模式glEnable(GL_DEPTH_TEST);glEnable(GL_CULL_FACE);}voidCube::resizeGL(intwidth,intheight){glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();GLfloatx=(GLfloat)width/height;glFrustum(-x,x,-1.0,1.0,4.0,15.0);glMatrixMode(GL_MODELVIEW);}voidCube::paintGL(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);draw(); //图的实际绘制的调用}voidCube::draw(){staticconstGLfloatcoords[6][4][3]={//6个面的坐标{{+1.0,-1.0,+1.0},{+1.0,-1.0,-1.0},{+1.0,+1.0,-1.0},{+1.0,+1.0,+1.0}},{{-1.0,-1.0,-1.0},{-1.0,-1.0,+1.0},{-1.0,+1.0,+1.0},{-1.0,+1.0,-1.0}},{{+1.0,-1.0,-1.0},{-1.0,-1.0,-1.0},{-1.0,+1.0,-1.0},{+1.0,+1.0,-1.0}},{{-1.0,-1.0,+1.0},{+1.0,-1.0,+1.0},{+1.0,+1.0,+1.0},{-1.0,+1.0,+1.0}},{{-1.0,-1.0,-1.0},{+1.0,-1.0,-1.0},{+1.0,-1.0,+1.0},{-1.0,-1.0,+1.0}},{{-1.0,+1.0,+1.0},{+1.0,+1.0,+1.0},{+1.0,+1.0,-1.0},{-1.0,+1.0,-1.0}}};glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(0.0,0.0,-10.0);glRotatef(rotationX,1.0,0.0,0.0);glRotatef(rotationY,0.0,1.0,0.0);glRotatef(rotationZ,0.0,0.0,1.0);for(inti=0;i<6;++i){glLoadName(i);glBegin(GL_QUADS);qglColor(faceColors[i]);//保存颜色来绘制正方体for(intj=0;j<4;++j){glVertex3f(coords[i][j][0],coords[i][j][1],coords[i][j][2]);}glEnd();}}voidCube::mousePressEvent(QMouseEvent*event){lastPos=event->pos();//鼠标点下的那个点作为轴点}voidCube::mouseMoveEvent(QMouseEvent*event) //鼠标移动时,正方体改变样式{

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论