如何在Qt Creator中添加自定义向导Wizards?
1、目的
标准化新建的Projects和Classes,也就是通过自定义模板来完成初始化。
2、使用
选择“File>New File or Project”或者点击“Ctrl+ N”打开向导,如下图所示:
(1)——类别。
(2)——图标。
(3)——名字。
(4)——描述。
3、参考
向导路径位于:(QtCreatorPath)/share/qtcreator/templates/wizards/
这个目录下有许多QtCreator内置的向导,可修改helloworld和listmodel目录下的wizard_sample.xml为wizard.xml来查看效果,自定义向导时可在这两个目录下的文件的基础上通过修改来完成。
4、自定义Project向导
(1)复制helloworld文件夹。
(2)修改wizard_sample.xml文件,最起码文件名应改为wizard.xml。
第一行代码:
<?xml version="1.0" encoding="UTF-8"?>
注释:
<!-- notes -->
(3)向导类别和位置
<wizard version="1" kind="project" class="qmakeproject" firstpage="10" id="A.HelloWorld" category="B.CustomProjects"> <!-- some codes --> </wizard>wizard—— 向导标签。
version——文件版本,不必修改。
kind——向导类型,可以是project和class,自定义Project向导时选择project。
class——可选项,kind为project时,指定project类型,值为qmakeproject时可添加Qt特定页面,如Kits配置页面。
firstpage——指定标准project向导中新页面的位置,一般为10。
id——向导的唯一标识,字母A指定了HelloWorld向导在category中的位置。
category——向导列表,字母B指定了CustomProjects在对话框中的位置。
(4)向导图标和内容
<icon>console.png</icon> <description>Creates a hello-world-project with custom message.</description> <displayname>Hello World</displayname>; <displaycategory>Custom Projects</displaycategory>
icon——向导图标。
description——向导描述。
displayname——向导名字。
displaycategor——向导类别。
另外,可指定向导内容语言种类。
<description xml:lang="de">Erzeugt ein Hello-Welt-Projekt mit einer Nachricht.</description> <displayname xml:lang="de">Hallo Welt</displayname> <displaycategory xml:lang="de">Benutzerdefinierte Projekte</displaycategory>(5) 添加文件
<files> <file source="main.cpp" openeditor="true" /> <file source="project.pro" target="%ProjectName%.pro" openproject="true" /> <file source="icon.png" target="%ProjectName%.png" binary="true" /> </files>
files——文件列表标签。
file——文件标签。
source——指定向导文件夹中复制到project的文件。
openeditor——设置文件是否在编辑器中打开。
target——文件重命名。
ProjectName——保存了创建project时的名字。
openproject——设置project文件是否打开。
binary——二进制文件,如图片。
另外,文件也可以由generate.pl脚本生成。
<generatorscript binary="generate.pl"> <argument value="--class-name=%ClassName%"/> <argument value="--project-name=%ProjectName%"/> <argument value="--header-suffix=%CppHeaderSuffix%" omit-empty="true"/> <argument value="--source-suffix=%CppSourceSuffix%" omit-empty="true"/> <argument value="--description=%Description%" omit-empty="true" write-file="true"/> </generatorscript>
(6)创建新的页面配置project
<fieldpagetitle>Hello World Parameters</fieldpagetitle> <fields> <field mandatory="true" name="MESSAGE"> <fieldcontrol class="QLineEdit" validator='^[^"]+$' defaulttext="Hello world!" placeholdertext="Enter a message"/> <fielddescription>Hello world message:</fielddescription> </field> </fields>fieldpagetitle—— 页面标题。
fields——页面上的UI对象列表标签。
field——单个对象标签。
mandatory——为true时强制性使用feild。
name——对象名字,可在模板文件中通过%MESSAGE%来使用。
fieldcontrol ——页面控制标签。
class—— fieldcontrol类型,如QLineEdit、QCheckBox、QComboBox、QTextEdit。
validator——正则表达式校验。
defaulttext——默认文本内容。
placeholdertext——提示信息。
fielddescription——对象描述。
5、自定义Class向导
与自定义Project向导类似,稍有不同。
(1)复制listmodel文件夹。
(2)修改wizard_sample.xml文件名为wizard.xml。
(3)向导描述
<wizard version="1" kind="class" id="A.ListModel" category="B.CustomClasses"> <description>Creates a QAbstractListModel implementation.</description> <displayname>QAbstractListModel implementation</displayname> <displaycategory>Custom Classes</displaycategory> <!-- some codes --> </wizard>
kind——值为Class。
(4)添加文件
<files> <file source="listmodel.cpp" target="%ClassName:l%.%CppSourceSuffix%" openeditor="true" /> <file source="listmodel.h" target="%ClassName:l%.%CppHeaderSuffix%" openeditor="true" /> </files>
%ClassName:l%—— ClassName为Class对象名字,修饰符l将其转换为小写。
%CppSourceSuffix%和%CppHeaderSuffix%——预定义的标准变量。
6、模板文件处理
(1)预定义标准变量
%ProjectName%
%Path%
%TargetPath%
%CppSourceSuffix%
%CppHeaderSufix%
%CurrentDate%
%CurrentTime%
%CurrentDate:Locale%
%CurrentTime:Locale%
%CurrentDate:ISO%
%CurrentTime:ISO%
%CurrentDate:RFC%
%CurrentTime:RFC%
(2)大小写转换
l ——小写。
u ——大写。
c ——首字母大写。