在加载图层的函数加载图层后,我怎么能调用函数?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在加载图层的函数加载图层后,我怎么能调用函数?相关的知识,希望对你有一定的参考价值。

我正在使用pyqt5制作Qgis 3.4插件,这个插件应该加载对o层并缩放到选定的插件,但是代码执行得太快并且在将图层加载到Qgis之前通过缩放线。

代码如下:

def openFileNameDialog(self):

    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Shapefile's (*.shp)", options=options)
    if fileName:
        shape_name = fileName

    shapelayer = QgsVectorLayer(shape_name, "project's shapefile", "ogr")
    if not shapelayer:
      self.statusBar().showMessage('shapefile is invalid')

    else:


        urlWithParams = 'url=urltoBaseMap'

        rlayer = QgsRasterLayer(urlWithParams, 'BaseMap', 'wms')

        if not rlayer.isValid():
            self.statusBar().showMessage('Layer failed to load!')

        QgsProject.instance().addMapLayer(rlayer)
        QgsProject.instance().addMapLayer(shapelayer)

        self.iface.setActiveLayer(shapelayer)

def layerzoom(self):

    self.iface.zoomToActiveLayer()

def runboth(self):

    self.openFileNameDialog()
    #time.sleep(5)
    self.layerzoom()

在加载图层的功能完成图层加载后,如何调用函数进行缩放?

我已经尝试过time.sleep()但是它没有等待openFileNameFialog()完成所以问题是坚持不懈的。

答案

将这两行添加到代码中可以等待图层加载zoomIn,这要归功于eyllanesc和ekhumoro的指导

def stop_signal():  self.layeropen()
self.iface.mapCanvas().renderComplete.connect(stop_signal)

代码看起来像这样:

def openFileNameDialog(self):

    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Shapefile's (*.shp)", options=options)
    if fileName:
        shape_name = fileName

    shapelayer = QgsVectorLayer(shape_name, "project's shapefile", "ogr")
    if not shapelayer:
      self.statusBar().showMessage('shapefile is invalid')

    else:


        urlWithParams = 'url=urltoBaseMap'

        rlayer = QgsRasterLayer(urlWithParams, 'BaseMap', 'wms')

        if not rlayer.isValid():
            self.statusBar().showMessage('Layer failed to load!')
        def stop_signal():  
            self.layeropen()
            self.istft = 'yes'

        QgsProject.instance().addMapLayer(rlayer)
        QgsProject.instance().addMapLayer(shapelayer)

        self.iface.mapCanvas().renderComplete.connect(stop_signal)

def layerzoom(self):
    if self.istft == 'yes':
        self.iface.zoomToActiveLayer()
    if self.istft != 'yes':
        pass

def mainprocess(self):

    self.istft = None

以上是关于在加载图层的函数加载图层后,我怎么能调用函数?的主要内容,如果未能解决你的问题,请参考以下文章

如何去除叠加图层后的多余形状

动态更改已加载 WMS 图层的样式

如图ARCMAP选项的栅格图层显示不全,如何解决?

PIE SDK图层渲染变化事件监听

如何使用已加载 KML 图层的动态 Google 地图上的按钮激活地理定位?

安卓高德地图开发java如何调用js自定义图层