您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > QGraphic-view实例
实验目的和要求1.采用Graphicview架构实现地图浏览器功能,支持QGraphicView和QGraphicsScene之间的坐标转换;支持QGraphicsScene坐标与地图坐标的转换;利用QGraphicsView的scale()实现地图的缩放。参考教材实例39.2.利用QGraphicsItem与定时器实现动画效果,参考教材实例42.以上两道程序任选一道实现即可。实验内容与分析设计NULL实验步骤与调试过程1.新建工程-Applications-QtGui应用,命名为mapwidget;2.新建选择-C++—C++源文件,命名为main.cpp;3.为main.cpp添加代码;4.同理添加源文件mapwidget.cpp;5.新建选择-C++—C++类,类命名为mapwidget;6.为mapwidget.h添加代码;7.为工程添加资源(图片);8.调试并运行程序。实验结果实现一个地图浏览器的基本功能,包括地图的浏览、放大、缩小,以及显示各点的坐标。疑难小结难点:图片的放大和缩小。主要算法和程序清单main.cpp#includeQApplication#includemapwidget.hintmain(intargc,char*argv[]){QApplicationapp(argc,argv);MapWidgetmap;map.show();returnapp.exec();}mapwidget.cpp#includemapwidget.h#includeQtGui#includemath.hMapWidget::MapWidget(){readMap();map.load(:/images/map.png);zoom=50;intwidth=map.width();intheight=map.height();QGraphicsScene*scene=newQGraphicsScene(this);scene-setSceneRect(-width/2,-height/2,width,height);setScene(scene);setCacheMode(CacheBackground);QSlider*slider=newQSlider;slider-setOrientation(Qt::Vertical);slider-setRange(1,100);slider-setTickInterval(10);slider-setValue(50);connect(slider,SIGNAL(valueChanged(int)),this,SLOT(slotZoom(int)));QLabel*zoominLabel=newQLabel;zoominLabel-setScaledContents(true);zoominLabel-setPixmap(QPixmap(:/images/zoomin.png));QLabel*zoomoutLabel=newQLabel;zoomoutLabel-setScaledContents(true);zoomoutLabel-setPixmap(QPixmap(:/images/zoomout.png));//createcoordinateareaQFrame*coordFrame=newQFrame;QLabel*label1=newQLabel(tr(GraphicsView:));viewCoord=newQLabel;QLabel*label2=newQLabel(tr(GraphicsScene:));sceneCoord=newQLabel;QLabel*label3=newQLabel(tr(map:));mapCoord=newQLabel;QGridLayout*grid=newQGridLayout;grid-addWidget(label1,0,0);grid-addWidget(viewCoord,0,1);grid-addWidget(label2,1,0);grid-addWidget(sceneCoord,1,1);grid-addWidget(label3,2,0);grid-addWidget(mapCoord,2,1);grid-setSizeConstraint(QLayout::SetFixedSize);coordFrame-setLayout(grid);//zoomlayoutQVBoxLayout*zoomLayout=newQVBoxLayout;zoomLayout-addWidget(zoominLabel);zoomLayout-addWidget(slider);zoomLayout-addWidget(zoomoutLabel);//coordinatearealayouQVBoxLayout*coordLayout=newQVBoxLayout;coordLayout-addWidget(coordFrame);coordLayout-addStretch();QHBoxLayout*layout=newQHBoxLayout;layout-addLayout(zoomLayout);layout-addLayout(coordLayout);layout-addStretch();layout-setMargin(30);layout-setSpacing(10);setLayout(layout);setWindowTitle(MapWidget);setMinimumSize(600,400);}//readmapinformationvoidMapWidget::readMap(){QFilemapFile(maps.txt);QStringmapName;intok=mapFile.open(QIODevice::ReadOnly);if(ok){QTextStreamt(&mapFile);if(!t.atEnd()){tmapName;tx1y1x2y2;}}map.load(mapName);if(map.isNull())printf(mapisnull);}voidMapWidget::slotZoom(intvalue){qreals;if(valuezoom)//zoomin{s=pow(1.01,(value-zoom));}else//zoomout{s=pow((1/1.01),(zoom-value));}scale(s,s);zoom=value;}voidMapWidget::drawBackground(QPainter*painter,constQRectF&rect){painter-drawPixmap(int(sceneRect().left()),int(sceneRect().top()),map);}voidMapWidget::mouseMoveEvent(QMouseEvent*event){//viewcoordinateQPointviewPoint=event-pos();viewCoord-setText(QString::number(viewPoint.x())+,+QString::number(viewPoint.y()));//scenecoordinateQPointFscenePoint=mapToScene(viewPoint);sceneCoord-setText(QString::number(scenePoint.x())+,+QString::number(scenePoint.y()));//mapcoordinateQPointFlatLon=mapToMap(scenePoint);mapCoord-setText(QString::number(latLon.x())+,+QString::number(latLon.y()));}//mapscenecoordinatetomapQPointFMapWidget::mapToMap(QPointFp){QPointFlatLon;qrealw=sceneRect().width();qrealh=sceneRect().height();qreallon=y1-((h/2+p.y())*abs(y1-y2)/h);qreallat=x1+((w/2+p.x())*abs(x1-x2)/w);latLon.setX(lat);latLon.setY(lon);returnlatLon;}mapwidget.h#ifndefMAPWIDGET_H#defineMAPWIDGET_H#includeQGraphicsViewclassQPixmap;classQLabel;classQPointF;classMapWidget:publicQGraphicsView{Q_OBJECTpublic:MapWidget();voidreadMap();QPointFmapToMap(QPointF);publicslots:voidslotZoom(int);protected:voiddrawBackground(QPainter*painter,constQRectF&rect);voidmouseMoveEvent(QMouseEvent*);private:QPixmapmap;qrealzoom;QLabel*viewCoord;QLabel*sceneCoord;QLabel*mapCoord;doublex1,y1;//maplefttoplon&latdoublex2,y2;//maprightbottomlon&lat};#endif//MAPWIDGET_H
本文标题:QGraphic-view实例
链接地址:https://www.777doc.com/doc-4564902 .html