After building a plugin using Plugin Builder I've encountered an issue where selections made in a result-loaded layer are not visible in canvas. I have to zoom in or out to refresh this layer. This problem persists despite attempts to force the canvas to refresh using the following lines of code:
iface.mapCanvas().refreshAllLayers()
iface.mapCanvas().refresh()
My Algorithm:
from qgis.PyQt.QtCore import QCoreApplication
from qgis.utils import iface
from qgis.core import (
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterNumber,
QgsProcessingParameterVectorLayer
)
class PluginTeste(QgsProcessingAlgorithm):
INPUT1 = 'INPUT1'
BUFFER1 = 'BUFFER1'
def initAlgorithm(self, config):
self.addParameter(
QgsProcessingParameterVectorLayer(
name = self.INPUT1,
description = 'Vetor INPUT',
defaultValue = 'TH_Geral',
types = [QgsProcessing.TypeVectorPolygon]
)
)
self.addParameter(
QgsProcessingParameterNumber(
name = self.BUFFER1,
description = 'Buffer (m)',
defaultValue = 100,
type = QgsProcessingParameterNumber.Integer
)
)
def processAlgorithm(self, parameters, context, feedback):
input_th_geral = self.parameterAsVectorLayer(parameters, self.INPUT1, context)
buffer_lim_th = self.parameterAsInt(parameters, self.BUFFER1, context)
buffer_result = processing.run("native:buffer", {
'INPUT': input_th_geral,
'DISTANCE': f'-{buffer_lim_th}',
'SEGMENTS': 5,
'END_CAP_STYLE': 0,
'JOIN_STYLE': 0,
'MITER_LIMIT': 2,
'DISSOLVE': False,
'SEPARATE_DISJOINT': False,
'OUTPUT': 'memory:TEMP Buffer'
}, context=context, feedback=feedback)['OUTPUT']
reprojection = processing.run('qgis:reprojectlayer', {
'INPUT': buffer_result,
'TARGET_CRS': QgsProject.instance().crs(),
'OUTPUT': 'memory:Reprojected'
}, context=context, feedback=feedback)
reprojection_result = reprojection['OUTPUT']
print(reprojection_result)
QgsProject.instance().addMapLayer(reprojection_result, True)
iface.mapCanvas().refreshAllLayers()
iface.mapCanvas().refresh()
return{}
def name(self):
return 'TESTE'
def displayName(self):
return self.tr(self.name())
def group(self):
return self.tr(self.groupId())
def groupId(self):
return 'Teste'
def shortHelpString(self):
return self.tr("""
Plugin para teste
""")
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return PluginTeste()