pyside6 qgis 显示图件
时间: 2024-04-22 14:17:42 浏览: 165
以下是使用PySide6和QGIS显示图件的示例代码:
```python
from PySide6.QtCore import Qt
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QApplication, QMainWindow, QToolBar, QAction
from qgis.core import QgsApplication, QgsVectorLayer, QgsMapLayerRegistry
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge
# 初始化QGIS应用程序
qgs = QgsApplication([], False)
qgs.initQgis()
# 创建主窗口和工具栏
app = QApplication([])
window = QMainWindow()
window.setWindowTitle("QGIS Map Viewer")
toolbar = QToolBar()
window.addToolBar(toolbar)
# 创建地图画布
canvas = QgsMapCanvas()
canvas.setCanvasColor(QColor(255, 255, 255))
bridge = QgsLayerTreeMapCanvasBridge(
QgsMapLayerRegistry.instance().rootGroup(), canvas)
bridge.setCanvasLayers()
# 添加图层
layer = QgsVectorLayer(
"path/to/your/vector/layer.shp", "Layer Name", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(layer)
# 添加工具栏按钮
zoom_in_action = QAction("Zoom In", window)
zoom_in_action.triggered.connect(lambda: canvas.zoomIn())
toolbar.addAction(zoom_in_action)
zoom_out_action = QAction("Zoom Out", window)
zoom_out_action.triggered.connect(lambda: canvas.zoomOut())
toolbar.addAction(zoom_out_action)
pan_action = QAction("Pan", window)
pan_action.setCheckable(True)
pan_action.triggered.connect(lambda: canvas.setMapTool(
canvas.PanTool if pan_action.isChecked() else None))
toolbar.addAction(pan_action)
# 将地图画布添加到主窗口
window.setCentralWidget(canvas)
window.show()
# 运行应用程序
app.exec_()
# 退出QGIS应用程序
qgs.exitQgis()
```
该示例代码使用PySide6创建了一个主窗口和工具栏,并在主窗口中添加了一个QgsMapCanvas对象,用于显示地图。示例代码还添加了三个工具栏按钮,用于缩放和平移地图。最后,示例代码使用QgsVectorLayer类添加了一个矢量图层。
阅读全文