【PyQt4实例10】地图浏览器
来源:互联网 发布:淘宝时尚自媒体怎么 编辑:程序博客网 时间:2024/05/22 12:02
#-*- coding:utf-8 -*-from PyQt4.QtCore import *from PyQt4.QtGui import *import mathQTextCodec.setCodecForTr(QTextCodec.codecForName("utf-8"))class MapWidget(QGraphicsView): def __init__(self): super(MapWidget,self).__init__() self.zoom = 50 self.map = QPixmap() self.x1 = QString() self.x2 = QString() self.y1 = QString() self.y2 = QString() self.readMap() self.width = self.map.width() self.height = self.map.height() self.scene = QGraphicsScene(self) self.scene.setSceneRect(-self.width/2,-self.height/2,self.width,self.height) self.setScene(self.scene) self.setCacheMode(self.CacheBackground) self.slider = QSlider() self.slider.setOrientation(Qt.Vertical) self.slider.setRange(1,100) self.slider.setTickInterval(10) self.slider.setValue(50) self.connect(self.slider,SIGNAL("valueChanged(int)"),self.slotZoom) self.zoominLabel = QLabel() self.zoominLabel.setScaledContents(True) self.zoominLabel.setPixmap(QPixmap("image/zoomin.png")) self.zoomoutLabel = QLabel() self.zoomoutLabel.setScaledContents(True) self.zoomoutLabel.setPixmap(QPixmap("image/deflate.png")) self.coordFrame = QFrame() self.label1 = QLabel(self.tr("GraphicsView:")) self.viewCoord = QLabel() self.label2 = QLabel(self.tr("GraphicsScene:")) self.sceneCoord = QLabel() self.label3 = QLabel(self.tr("map:")) self.mapCoord = QLabel() self.grid = QGridLayout() self.grid.addWidget(self.label1,0,0) self.grid.addWidget(self.viewCoord,0,1) self.grid.addWidget(self.label2,1,0) self.grid.addWidget(self.sceneCoord,1,1) self.grid.addWidget(self.label3,2,0) self.grid.addWidget(self.mapCoord,2,1) self.grid.setSizeConstraint(QLayout.SetFixedSize) self.coordFrame.setLayout(self.grid) self.zoomLayout = QVBoxLayout() self.zoomLayout.addWidget(self.zoominLabel) self.zoomLayout.addWidget(self.slider) self.zoomLayout.addWidget(self.zoomoutLabel) self.coordLayout = QVBoxLayout() self.coordLayout.addWidget(self.coordFrame) self.coordLayout.addStretch() self.layout = QHBoxLayout() self.layout.addLayout(self.zoomLayout) self.layout.addLayout(self.coordLayout) self.layout.addStretch() self.layout.setMargin(30) self.layout.setSpacing(10) self.setLayout(self.layout) self.setWindowTitle(self.tr("地图浏览器")) self.setMinimumSize(600,400) def drawBackground(self,painter,rect): painter.drawPixmap(int(self.sceneRect().left()),int(self.sceneRect().top()),self.map) def readMap(self): mapFile = QFile("maps.txt") mapName = QString() ok = mapFile.open(QIODevice.ReadOnly) if(ok is True): t = QTextStream(mapFile) if(t.atEnd() is False): t >> mapName t >> self.x1 >> self.y1 >> self.x2 >> self.y2 self.map.load(mapName) def slotZoom(self,v): if(v > self.zoom): s = math.pow(1.01,(v - self.zoom)) else: s = math.pow((1/1.01),(self.zoom - v)) self.scale(s,s) self.zoom = v def mouseMoveEvent(self,event): viewPoint = event.pos() self.viewCoord.setText(QString.number(viewPoint.x()) + "," + QString.number(viewPoint.y())) scenePoint = self.mapToScene(viewPoint) self.sceneCoord.setText(QString.number(scenePoint.x()) + "," + QString.number(scenePoint.y())) latLon = self.mapToMap(scenePoint) self.mapCoord.setText(QString.number(latLon.x()) + "," + QString.number(latLon.y())) def mapToMap(self,p): latLon = QPointF() w = self.sceneRect().width() h = self.sceneRect().height() self.x1= float(self.x1) self.x2= float(self.x2) self.y1= float(self.y1) self.y2= float(self.y2) lon = self.y1 - ((h/2 + p.y()) * abs(self.y1 - self.y2)/h) lat = self.x1 + ((w/2 + p.x()) * abs(self.x1 - self.x2)/w) latLon.setX(lat) latLon.setY(lon) return latLon if __name__ == '__main__': import sys app = QApplication(sys.argv) mainwindow = MapWidget() mainwindow.show() sys.exit(app.exec_())
上述代码中的maps.txt文件如下:
aa.png 114.4665527 35.96022297 119.9597168 31.3911575
0 0
- 【PyQt4实例10】地图浏览器
- 【PyQt4 实例24】文件浏览器
- 【Pyqt4 实例14】文件目录浏览器
- PyQt4 实例源码阅读
- pyqt4实例练习1
- pyqt4实例练习2
- 【PyQt4实例9】橡皮筋线
- 【PyQt4 实例27】事件过滤器
- PyQT4图形界面开发入门实例
- PyQT4图形界面开发入门实例
- [转]*PyQt4 精彩实例分析* 实例10 …
- PyQt4 精彩实例分析* 实例10 在程序中使用Ui
- PyQt4 精彩实例分析* 实例22 电子钟
- 收集了一个pyqt4做的浏览器
- Pyqt4
- pyqt4
- 【PyQt4实例11】各种Graphics Items
- 【Pyqt4 实例13】飞舞的蝴蝶
- LCA(离线算法)
- mysql高可用探究(六)MMM高可用mysql方案
- 构建链表进行冒泡排序,翻转,链表合并测试
- 2K分辨率有多高清 手机是否需要2K屏幕
- Android 内存溢出解决方案(OOM) 整理总结
- 【PyQt4实例10】地图浏览器
- Unity3d 面向对象设计思想(二)(动态UI的分配与调度)
- Sqlite3中replace语句用法详解
- SQL 处理数据 curd
- [SDK]新浪微博请求授权显示错误页面:请您联系***
- MYSQL高可用方案探究(七)参考文献
- jquery 让div滚动条自动滚动到最下面
- [NodeJS] 优缺点及适用场景讨论
- 最近在忙些什么