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.
Open the QGIS Python Console by selecting the Plugins menu and then clicking on Python Console.
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:
from PyQt4.QtCore import * from PyQt4.QtGui import *
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>")
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)
layout = QVBoxLayout() layout.addWidget(self.label) layout.addWidget(self.uname) self.setLayout(layout)
class Page2(QWizardPage): def __init__(self, parent=None): super(Page2, self).__init__(parent) self.setTitle("When's Your Birthday?") self.setSubTitle("Select Your Birthday.")
self.cal = QCalendarWidget()
field
, to be accessed later on: self.registerField("cal", self.cal, "selectedDate")
layout = QVBoxLayout() layout.addWidget(self.cal) self.setLayout(layout)
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)
def initializePage(self): uname = self.field("uname") date = self.field("cal").toString()
self.name_lbl.setText("Your name is %s" % uname) self.date_lbl.setText("Your birthday is %s" % date)
wiz = QWizard() wiz.addPage(Page1()) wiz.addPage(Page2()) wiz.addPage(Page3()) wiz.show()
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: