Stepping the user through a wizard

A wizard is a series of dialogs that lead the user through a sequence of steps. The information on each page of a wizard might relate in some way to the information on other pages. In this recipe, we'll create a simple three-page wizard to collect some information from the user and display it back to them.

Getting ready

Open the QGIS Python Console by selecting the Plugins menu and then clicking on Python Console.

How to do it...

We will create three classes, each representing a page of our wizard. The first two pages will collect information and the third page will display it back to the user. We will create a QWizard object to tie the page classes together. We will also use the concept of wizard fields to pass information among the pages.

To do this, we need to perform the following steps:

  1. First, we import the GUI and QGIS core libraries:
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
  2. Next, we create the class for the first page of our wizard and add a textbox to collect the user's name as the uname variable:
    class Page1(QWizardPage):
        def __init__(self, parent=None):
            super(Page1, self).__init__(parent)
            self.setTitle("What's Your Name?")
            self.setSubTitle("Please enter your name.")
            self.label = QLabel("Name:")
            self.uname = QLineEdit("<enter your name>")
    
  3. Now, we register the uname field so that we'll be able to access the entered value later on, without having to keep track of the variable itself:
            self.registerField("uname", self.uname)
    
  4. Then, we set up the layout for the page:
            layout = QVBoxLayout()
            layout.addWidget(self.label)
            layout.addWidget(self.uname)
            self.setLayout(layout)
    
  5. Next, we'll set the class for our second page:
    class Page2(QWizardPage):
        def __init__(self, parent=None):
            super(Page2, self).__init__(parent)
            self.setTitle("When's Your Birthday?")
            self.setSubTitle("Select Your Birthday.")
    
  6. Then, we'll add a calendar widget to get the user's birthday:
            self.cal = QCalendarWidget()
    
  7. We'll register the selected date as a field, to be accessed later on:
            self.registerField("cal", self.cal, "selectedDate")
    
  8. Then, we'll set up the layout for this page:
            layout = QVBoxLayout()
            layout.addWidget(self.cal)
            self.setLayout(layout)
    
  9. We are now ready to set up the third page, which will display the user's information. We'll use simple labels, which are dynamically populated in the next step:
    class Page3(QWizardPage):
        def __init__(self, parent=None):
            super(Page3, self).__init__(parent)
            self.setTitle("About You")
            self.setSubTitle("Here is Your Information:")
            self.name_lbl = QLabel()
            self.date_lbl = QLabel()
            layout = QVBoxLayout()
            layout.addWidget(self.name_lbl)
            layout.addWidget(self.date_lbl)
            self.setLayout(layout)
    
  10. Now, we set up the initialization of the page. We will first access the fields registered from the previous pages to grab the user input:
        def initializePage(self):
            uname = self.field("uname")
            date = self.field("cal").toString()
    
  11. Then, all we have to do is set those values to the text for the labels using Python string formatting:
            self.name_lbl.setText("Your name is %s" % uname)
            self.date_lbl.setText("Your birthday is %s" % date)
    
  12. Finally, we create our wizard widget, add pages, and display the wizard:
    wiz = QWizard()
    wiz.addPage(Page1())
    wiz.addPage(Page2())
    wiz.addPage(Page3())
    wiz.show() 
    

How it works...

The wizard interface shares many traits with the tab widget, with some important differences. The wizard only allows the user to move back and forth in a linear progression based on the page order. It can share information among pages if the information is registered as fields, which then makes the pages global to the scope of the wizard. However, the field() method is a protected method, so your pages must be defined as classes inherited from the QWizardPage object for the registered fields to work as expected. The following screenshot shows the calendar screen of the wizard:

How it works...
..................Content has been hidden....................

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