本页内容

在代码中创建向导

简介

如果基于模板的自定义向导所提供的功能无法满足您的需求,您可以通过代码编写向导。

在Qt Creator 中,向导是实现Core::IWizardFactory 接口的类的一个实例,该类拥有一个已通过IWizardFactory::registerFactoryCreator注册的创建函数。

实现向导需要:

  • 编写一个从 `Core::IWizardFactory` 派生的工厂类。这是一个非常通用的接口,它不对向导的功能及其用户界面做出任何假设。
  • 提供一组参数,用于确定向导在New File 或New Project 对话框的向导列表中如何显示。

    当从 `Core::IWizardFactory` 派生时,构造函数必须调用基类提供的以下设置器:

    • setId
    • setWizardKind
    • setIcon
    • setDescription
    • setDisplayName
    • setCategory
    • setDisplayCategory
    • setDescriptionImage
    • setRequiredFeatures
    • setFlags
  • 实现向导用户界面

    通常,这将是一个从Utils::Wizard 派生的类。Utils::Wizard 继承自QWizard ,并增加了在左侧显示进度条的功能。

  • 实现向导功能

    建议使用 `Core::GeneratedFile ` 来表示需要写入磁盘的文件。它们允许将实际数据的写入延迟到向导完成之后。

相关类

类描述
Core::IWizardFactoryQt Creator 向导接口,其实现类需向ExtensionSystem::PluginManager 进行注册。
Core::GeneratedFile一个包含名称、内容和某些属性的文件。
Utils::FileWizardPage引导向导页面,用于询问文件名和路径。
Utils::ProjectIntroPage向用户询问项目名称和路径的向导介绍页面。

IWizardFactory 的设置器和获取器

下面列出的 setter 和 getter 方法决定了向导在New File 或New Project 对话框中的向导列表中如何显示。

类型参数名称描述
Core::IWizardFactory::WizardKindkind表示向导类型的枚举值(project 或file )。
QIconicon要显示的图标。
QStringdescription描述性文本。
QStringdisplayName要在列表中显示的名称。
QStringid向导的唯一标识符。它还决定了该向导在类别中的排序顺序。
QStringcategory向导所属类别的标识符。它还决定了各类别之间的排序顺序。
QStringdisplayCategory类别的描述。

所有设置了相同类别的向导,都将在“New File ”或“New Project ”对话框中分组显示。

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.