1. 界面效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4dfeb84d79cb02e89e263ee68aa066c2.png)
2. Python代码(.py)
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from qgis.core import *
from qgis.gui import *
from qgis.PyQt.QtGui import *
import sys
import os
from ui_demo import Ui_MainWindow
class MyWnd(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.project = QgsProject.instance()
self.canvas = QgsMapCanvas(self.canvasFrame)
self.canvas.setCanvasColor(Qt.white)
self.vl_canvasFrame.addWidget(self.canvas)
self.layer_tree_view = QgsLayerTreeView(self)
self.vl_layerDock.addWidget(self.layer_tree_view)
self.layer_tree_root = self.project.layerTreeRoot()
self.layer_tree_model = QgsLayerTreeModel(self.layer_tree_root)
self.layer_tree_model.setFlag(QgsLayerTreeModel.AllowNodeRename)
self.layer_tree_model.setFlag(QgsLayerTreeModel.AllowNodeReorder)
self.layer_tree_model.setFlag(QgsLayerTreeModel.AllowNodeChangeVisibility)
self.layer_tree_model.setFlag(QgsLayerTreeModel.ShowLegendAsTree)
self.layer_tree_model.setAutoCollapseLegendNodes(10)
self.layer_tree_view.setModel(self.layer_tree_model)
self.layer_tree_bridge = QgsLayerTreeMapCanvasBridge(self.layer_tree_root, self.canvas)
crs = QgsCoordinateReferenceSystem("EPSG:4326")
self.canvas.setDestinationCrs(crs)
path_to_tif = r"path/to/tif"
self.rlayer = QgsRasterLayer(path_to_tif, "LAYER NAME")
self.project.addMapLayer(self.rlayer)
self.canvas.setCurrentLayer(self.rlayer)
self.canvas.setExtent(self.rlayer.extent())
self.single_band_pseudo_color_renderer_widget = QgsSingleBandPseudoColorRendererWidget(
self.canvas.currentLayer()
)
self.vl_attributeDock.addWidget(self.single_band_pseudo_color_renderer_widget)
self.single_band_pseudo_color_renderer_widget.widgetChanged.connect(self.update_renderer_widget)
self.actionZoomIn = QAction("Zoom in", self)
self.actionZoomOut = QAction("Zoom out", self)
self.actionPan = QAction("Pan", self)
self.actionZoomIn.setCheckable(True)
self.actionZoomOut.setCheckable(True)
self.actionPan.setCheckable(True)
self.actionZoomIn.triggered.connect(self.zoomIn)
self.actionZoomOut.triggered.connect(self.zoomOut)
self.actionPan.triggered.connect(self.pan)
self.toolbar = self.addToolBar("Canvas actions")
self.toolbar.addAction(self.actionZoomIn)
self.toolbar.addAction(self.actionZoomOut)
self.toolbar.addAction(self.actionPan)
self.toolPan = QgsMapToolPan(self.canvas)
self.toolPan.setAction(self.actionPan)
self.toolZoomIn = QgsMapToolZoom(self.canvas, False)
self.toolZoomIn.setAction(self.actionZoomIn)
self.toolZoomOut = QgsMapToolZoom(self.canvas, True)
self.toolZoomOut.setAction(self.actionZoomOut)
self.pan()
def update_renderer_widget(self):
print('Update Renderer ... ')
self.single_band_pseudo_color_renderer_widget.doComputations()
self.rlayer.setRenderer(self.single_band_pseudo_color_renderer_widget.renderer())
self.rlayer.triggerRepaint()
def zoomIn(self):
self.canvas.setMapTool(self.toolZoomIn)
def zoomOut(self):
self.canvas.setMapTool(self.toolZoomOut)
def pan(self):
self.canvas.setMapTool(self.toolPan)
if __name__ == '__main__':
qgs = QgsApplication([], True)
qgs.setPrefixPath('qgis', True)
qgs.initQgis()
window = MyWnd()
window.show()
exit_code = qgs.exec_()
qgs.exitQgis()
sys.exit(exit_code)
3. 演示Ui代码(.py)
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from qgis.gui import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(1001, 755)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(u"gridLayout")
self.layerDockWidget = QgsDockWidget(self.centralwidget)
self.layerDockWidget.setObjectName(u"layerDockWidget")
self.dockWidgetContents = QWidget()
self.dockWidgetContents.setObjectName(u"dockWidgetContents")
self.layerDockWidget.setWidget(self.dockWidgetContents)
self.vl_layerDock = QVBoxLayout(self.dockWidgetContents)
self.canvasFrame = QFrame(self.centralwidget)
self.canvasFrame.setObjectName(u"canvasFrame")
self.canvasFrame.setFrameShape(QFrame.StyledPanel)
self.canvasFrame.setFrameShadow(QFrame.Raised)
self.vl_canvasFrame = QVBoxLayout(self.canvasFrame)
self.attributeDockWidget = QgsDockWidget(self.centralwidget)
self.attributeDockWidget.setObjectName(u"attributeDockWidget")
self.dockWidgetContents_2 = QWidget()
self.dockWidgetContents_2.setObjectName(u"dockWidgetContents_2")
self.attributeDockWidget.setWidget(self.dockWidgetContents_2)
self.vl_attributeDock = QVBoxLayout(self.dockWidgetContents_2)
self.gridLayout.addWidget(self.canvasFrame, 0, 0, 2, 1)
self.gridLayout.addWidget(self.layerDockWidget, 0, 1, 1, 1)
self.gridLayout.addWidget(self.attributeDockWidget, 1, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 1001, 26))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))