Creating Wizards in Code
Introduction
If the functionality provided by template-based custom wizards is not sufficient for your case, you can write wizards in code.
A wizard in Qt Creator is an instance of a class implementing the Core::IWizard interface that is registered with ExtensionSystem::PluginManager.
Implementing wizards requires:
- Deciding on a base class:
- Core::IWizard is a very generic interface that does not make any assumption about what the wizard does and what its UI looks like.
- Core::BaseFileWizard should be used for wizards that generate files using a UI based on Utils::Wizard.
- Providing a set of parameters that determine how the wizard shows up in the list of wizards in the New File or Project dialog.
When deriving from Core::IWizard, virtual functions returning the values have to be implemented.
When deriving from Core::BaseFileWizard, a parameter class Core::BaseFileWizardParameters needs to be passed to the constructor, on which the parameters can be set. This allows for easy creation of several wizard instances with slightly different parameters.
- Implementing the wizard UI
Typically, this will be a class derived from Utils::Wizard. Utils::Wizard extends QWizard with the functionality to show a progress bar on the left.
- Implementing the wizard functionality
When deriving from Core::BaseFileWizard, a list of Core::GeneratedFile needs to be populated with the files and their contents.
Note: The files are not actually written to the disk. This will be done by Core::BaseFileWizard after performing overwrite checks and prompting the user accordingly.
Relevant Classes
Class | Description |
---|---|
Core::IWizard | Qt Creator wizard interface, implementations of which are registered with ExtensionSystem::PluginManager. |
Core::BaseFileWizard | Inherits Core::IWizard and provides a base class for generating files with a UI based on QWizard. |
Core::BaseFileWizardParameters | Contains parameters for Core::BaseFileWizard. |
Core::GeneratedFile | A file as produced by Core::BaseFileWizard, containing name, contents and some attributes. |
Utils::FileWizardPage | Introductory wizard page asking for file name and path. |
Utils::FileWizardDialog | A wizard dialog presenting a Utils::FileWizardPage, which can be extended by custom pages. |
Utils::ProjectIntroPage | Introductory wizard page asking for project name and path. |
ProjectExplorer::BaseProjectWizardDialog | Base class for project wizard dialogs, presenting a Utils::ProjectIntroPage. |
Parameters
The parameters listed below determine how the wizard shows up in the list of wizards in the New File or Project dialog.
Wizards in Qt Creator are grouped by categories.
Type | Parameter Name | Description |
---|---|---|
Core::IWizard::WizardKind | kind | Enumeration value that indicates the type of the wizard (project, class, file). |
QIcon | icon | Icon to show. |
QString | description | Descriptive text. |
QString | displayName | Name to be shown in the list. |
QString | id | Unique identifier for the wizard. It also determines the order within a category. |
QString | category | Identifier of the category under which the wizard is to be listed. It also determines the order of the categories. |
QString | displayCategory | Description of the category. |
Example
Introduction
In this example, we create a wizard for writing HTML files consisting of a title and a paragraph, making use of QXmlStreamWriter.
For the UI, we use Utils::FileWizardDialog and extend it by a page letting the user enter title and contents.
In our BaseFileWizard implementation, we create the file contents using QXmlStreamWriter.
The WebContentPageWizardPage Class
Let us start with the wizard page. We use a QLineEdit for the title and a QPlainTextEdit for the content, arranged in a QGridLayout with descriptive labels.
Note: The QGridLayout was chosen to be able to accommodate the large vertical span of the QPlainTextEdit. For standard controls, a QFormLayout should be considered, which will lay out the labels according to the platform guide lines.
On top of that, we implement validation logic to ensure content is entered. We implement QWizardPage::isComplete() to return true when both input widgets have contents, enabling the Next button. For this to happen as the user enters text, we need to connect to the changed() signal of the controls and emit QWizardPage::completeChanged() once the complete status changes.
The WebContentWizardDialog Class
The wizard dialog extends Utils::FileWizardDialog, which presents an introductory page asking for file name and path. We add our WebContentPageWizardPage after that.
The WebContentWizard Class
In our implementation of Core::BaseFileWizard, we overwrite createWizardDialog() to return an instance of our WebContentWizardDialog, initially set to the path passed in. We also add the extension pages we receive. Extension pages are for example the wizard pages asking for a project to add the files to and whether to add the files to a version control system.
In generateFiles(), we obtain the parameters from our wizard and populate the list of Core::GeneratedFile with our file. To generate the contents, we use QXmlStreamWriter.
Plugin Registration
In order for the wizard to be found by the New dialog, we need to register it with ExtensionSystem::PluginManager, which also takes care of deleting it:
Complete Example Code
Here is the complete code of webpagewizard.h:
The complete code of webpagewizard.cpp looks as follows:
The registration of the wizard in the initialize() function of a plugin looks like: