3

I developed a plugin under QGIS and I added a progressBar using QT Designer. Using a pushButton, I execute several functions directly in the __init__(self, iface) function. What I want to do is to have a real progression of function execution time and observe it in the GUI of the plugin (bar progression with percents)

Below a different function's execution :

    self.dlg.pushButton_OK.clicked.connect(self.progressBar_test)
    self.dlg.pushButton_OK.clicked.connect(self.recup_indiceColonnesTDJ)
    self.dlg.pushButton_OK.clicked.connect(self.move_in_group)
    self.dlg.pushButton_OK.clicked.connect(self.recup_ftta)
    self.dlg.pushButton_OK.clicked.connect(self.cheminement_carto)
    self.dlg.pushButton_OK.clicked.connect(self.recup_EtatDNCapillaire)
    self.dlg.pushButton_OK.clicked.connect(self.recup_EtatOMEGA)
    self.dlg.pushButton_OK.clicked.connect(self.recup_coord_cheminTrans)
    self.dlg.pushButton_OK.clicked.connect(self.recup_dict_coordAncrage)
    self.dlg.pushButton_OK.clicked.connect(self.layer_fusion)
    self.dlg.pushButton_OK.clicked.connect(self.chargement_liaisons)
    self.dlg.pushButton_OK.clicked.connect(self.recup_EtatMigrationPort10G)
    self.dlg.pushButton_OK.clicked.connect(self.recup_TDJ_5G_TypeTrans)
    self.dlg.pushButton_OK.clicked.connect(self.recup_TDJ_5G)

I have tried all the suggestions to the various questions asked about this but I have not found a question that directly addresses the time elapsed while execution of different functions in the same time.

Does anyone have a solution to my problem?

1 Answer 1

6

Add a variable to the first function to store starting time. For example (self.start). Define a new function to set progress. Then use that function instead of setValue in other functions called by clicked event of the push button.

from time import time, gmtime, strftime

class class_name(...):

    def __init__(self, iface):
        ...

    def set_progress(self, progress_value):
        t = time() - self.start
        hms = strftime("%H:%M:%S", gmtime(t))
        
        self.progressBar.setValue(progress_value)
        self.progressBar.setFormat(f"{hms} - %p%")


    # the first function called by clicked event
    def progressBar_test(self):

        self.start = time()

        ...
        self.set_progress(<Value>) # instead of self.progressBar.setValue(<Value>)
        ...

    # another function called by clicked event
    def other_function1(self):

        # use just set_progress
        self.set_progress(<Value>)
        ...

enter image description here
enter image description here
enter image description here

2
  • Thank you @Kadir for your help, I have just one question please: What is th parameter '<Value>' in 'self.set_progress(<Value>)'? Thank you
    – nmokht97
    Feb 22, 2022 at 11:03
  • 1
    Value-> integer. 0, 1, 2, …, 100. Feb 22, 2022 at 11:09

Your Answer

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

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