Displaying a progress bar

A progress bar is a dynamic dialog that displays the percentage complete bar for a running process that the user must wait for before continuing. A progress bar is more advanced than a simple dialog because it needs to be updated continuously. In this recipe, we'll create a simple progress dialog based on a timer.

Getting ready

No groundwork is required for this recipe.

How to do it...

The steps for this recipe include creating a custom class based on the QProgressBar, initializing the dialog and setting its size and title, creating a timer, connecting the progress bar to the timer, starting the time, and displaying the progress. To do this, we need to perform the following steps:

  1. First, we must import both the GUI and QGIS core libraries:
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    
  2. Next, we create a custom class for our progress bar, including a method to increase the value of the progress bar:
    class Bar(QProgressBar):
      value = 0
      def increaseValue(self):
        self.setValue(self.value)
        self.value = self.value+1
    
  3. Now, we set the progress bar:
    bar = Bar()
    
  4. Next, we set the progress bar's size and title:
    bar.resize(300,40)
    bar.setWindowTitle('Working...')
    
  5. Then, we initialize the timer, which will serve as the process we monitor:
    timer = QTimer()
    
  6. Now, connect the the timer's timeout signal to the increaseValue method, which we created earlier. Whenever the timer finishes its countdown, it will emit the timeout signal and notify the increaseValue method.
    timer.timeout.connect(bar.increaseValue)
    
  7. Now, we will start the timer, specifying an interval of 500 milliseconds. The timer will call its timeout() signal every 0.5 seconds:
    timer.start(500)
    
  8. Finally, we show the progress bar and start the progress meter:
    bar.show()
    

How it works...

The progress bar will stop when its value reaches 100, but our timer will continue to run until the stop() method is called. In a more realistic implementation, you will need a way to determine whether the monitored process is complete. The indicator might be the creation of a file, or even better, a signal. The Qt framework uses the concept of signals and slots to connect GUI elements. A GUI is event-based, with multiple events occurring at different times, including user actions and other triggers. The signal/slot system allows you to define reactions to events when they occur, without writing code to continuously monitor changes. In this recipe, we use the predefined signal from the timer and create our own slot. A slot is just a method identified as a slot by passing it to a signal's connect() method. The following screenshot shows an example of the progress bar:

How it works...

There's more…

In a complex GUI application such as QGIS, you will end up with multiple signals that trigger multiple slots simultaneously. You must take care that a rapidly updating element such as a progress bar doesn't slow down the application. Using a thread to only update the progress bar when something has truly changed is more efficient. For an example of this technique, take a look at http://snorf.net/blog/2013/12/07/multithreading-in-qgis-python-plugins/.

Using the QgsMessageBar object is preferred to display informative messages, but it can also accept widgets such as the progress bar. The PyQGIS Developer Cookbook has an example that shows how to place the progress bar in the QgsMessageBar object (http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/communicating.html)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset