我正在使用pyqt5制作一个Qgis 3.4插件,该插件应该加载成对的图层并缩放到选定的图层,但是代码执行得太快,并且在将图层加载到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()完成,所以问题仍然存在。
答案 0 :(得分:0)
在代码中添加这两行代码,就可以等待图层加载到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