3

I am trying to show the progress in the progress bar, but it is not running, I have seen several solutions but none has solved me.

This is part of the code that I am using :

def progress_changed(self, progress):
    self.progressalg.setValue(progress)


def AdicionarDatosparcelas(self):
    self.usuario_origen = self.cfgdlg.v_usuario_o.text()
    self.contrasena_origen = self.cfgdlg.v_contrasena_o.text()
    self.usuario_destino = self.cfgdlg.v_usuario_d.text()
    self.contrasena_destino = self.cfgdlg.v_contrasena_d.text()

    progressMessageBar = self.iface.messageBar().createMessage("Insertando parcelas en la base de datos...")
    self.progressalg = QProgressBar()
    self.progressalg.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
    progressMessageBar.layout().addWidget(self.progressalg)
    self.iface.messageBar().pushWidget(progressMessageBar, Qgis.Info)

    f = QgsProcessingFeedback()
    f.progressChanged.connect(self.progress_changed)        

    geom_column = None
    uri_origen = QgsDataSourceUri()
    uri_origen.setConnection(self.servidor_origen, str(self.puerto_origen), self.basedato_origen, self.usuario_origen,
                             self.contrasena_origen,
                             QgsDataSourceUri.SslDisable)
    uri_origen.setParam('key', 'mi_prinx')
    uri_origen.setParam('checkPrimaryKeyUnicity','0')


    uri_origen.setDataSource("catastro", "parcelas", geom_column)
    parcela_origen = QgsVectorLayer(uri_origen.uri(), 'parcela_origen', "postgres")
    QgsProject.instance().addMapLayer(parcela_origen, True)

    uri_destino = QgsDataSourceUri()
    uri_destino.setConnection(self.servidor_destino, str(self.puerto_destino), self.basedato_destino,
                              self.usuario_destino, self.contrasena_destino,
                              QgsDataSourceUri.SslDisable)

    uri_destino.setParam('key', 'mi_prinx')
    uri_origen.setParam('checkPrimaryKeyUnicity', '0')
    uri_destino.setDataSource("catastro", "parcelas", geom_column)
    parcela_destino = QgsVectorLayer(uri_destino.uri(), 'parcela_destino', "postgres")
    QgsProject.instance().addMapLayer(parcela_destino, True)

    processing.run("etl_load:appendfeaturestolayer", {
        'SOURCE_LAYER': parcela_origen,
        'SOURCE_FIELD': 'mi_prinx',
        'TARGET_LAYER': parcela_destino,
        'TARGET_FIELD': 'mi_prinx',
        'ACTION_ON_DUPLICATE': 1}, feedback=f)
    self.iface.messageBar().pushSuccess('parcelas exportadas', '')

1 Answer 1

2

Not sure I can help directly but you can run the following as it perfectly shows the progress bar and the progressChanged calls correctly the progress_changed function (tested before answering)

from qgis.core import QgsProcessingContext, QgsProcessingFeedback, QgsProject

bar = QProgressBar()
bar.setRange(0,100)
iface.mainWindow().statusBar().addWidget(bar)
# Could be replaced by the following if you want same position as the default QGIS component for progress bar
# face.mainWindow().statusBar().insertPermanentWidget(0, bar)

alg_params = { 'FIELD' : 'fid', 'INPUT' : '/usr/share/qgis/resources/data/world_map.gpkg|layername=countries', 'OPERATOR' : 4, 'OUTPUT' : 'TEMPORARY_OUTPUT', 'VALUE' : '1000' }

def progress_changed(progress):
    bar.setValue(progress)

feedback = QgsProcessingFeedback()
feedback.progressChanged.connect(progress_changed)

context = QgsProcessingContext()
extracted = processing.run('native:extractbyattribute', alg_params, context=context, feedback=feedback, is_child_algorithm=False)

# Comment if you want to be sure to see the progress bar position before removing it
iface.mainWindow().statusBar().removeWidget(bar)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.