You get a bonus - 1 coin for daily activity. Now you have 1 coin

Project management. Creating a Qt Project

Lecture



You use wizards to create and import certain types of projects and files, such as the GUI or the Qt console application. You can also use wizards to add individual files to your projects. For example, you can create the following file types:

  • The Qt resource file, which allows you to store binary files in an executable application file
  • Qt Designer Forms and Qt QML Files That Describe Parts of an Application User Interface
  • C ++ class, source file or header file

Wizards will ask you for the necessary settings for a particular project and create the necessary files for you. To display other types of files in the Projects panel, specify them in the project file.

Using the project wizard

To create a new project:

  1. Select File > New File or Project ... and select your project type.
    The contents of the dialogs will depend on the type of project. Follow the wizard instructions. This example uses the Qt GUI application .
  2. Name the project and select its path. To select a path from the directory tree, click Browse ...

    Avoid using spaces and special characters in the project name and path.

  3. Specify the name of the class you want to create and, using the drop-down menu, select the type of base class.
    Note that the Header File , Source File and Form File fields will be updated automatically according to the class name you selected.
  4. Check project settings.
    To create a project, click Finish .

Display additional file types in the project pane

Qt Creator determines whether to display files from the project directory in the Projects panel depending on the file type (.pro, .pri, .cpp, .h, .ui, .qrc, and so on). To display other types of files, modify the project file. Add file names as the value of the OTHER_FILES variable. You can also use wildcard characters.

For example, the following code indicates that text files should appear in the Projects panel:

OTHER_FILES + = * .txt

It also ensures the availability of these files in the Search Engine .

Adding new project masters

If you have team work on a large application or several applications, you may want to standardize how team members create projects and classes.

You can use the wizard template from the share / qtcreator / templates / wizards directory to create your own project and class wizards. Qt Creator scans this directory and adds all the wizards defined in the wizard.xml file to the New dialog that opens when you select File> New File or Project .

In the project wizard, you can specify the necessary project files. You can add wizard pages to allow developers to specify project settings.

In the class wizard, you can allow developers to specify the class name, base class and header files and source files for the class.

To see how this works, rename wizard_example.xml to wizard.xml in the helloworld and listmodels directories. After you restart Qt Creator, the categories Own classes and Own projects appear in the New dialog.

Creating project masters

To create a project wizard:

  1. Make a copy of the catalog
    share / qtcreator / templates / wizards / helloworld
    or
    share / qtcreator / templates / wizards / listmodel
  2. Modify the wizard_example.xml file
  3. The following code defines the type of wizard and places it in the New dialog:
    <wizard version = "1" kind = "project"
    class = "qt4project" firstpage = "10"
    id = "A.HelloWorld" category = "B.CustomProjects">
    • version is the version of the file contents. Do not change this value.
    • kind indicates the type of project: project or class.
    • class indicates the type of project. Currently, only the qt4project type, which describes the Qt console application, is available.
    • firstpage indicates the location of the new page in the standard project wizard. A value of 10 ensures that the page created appears after the standard pages as the last page of the wizard.
    • id is your master's unique id. The letter indicates the position of the master in the category. The HelloWorld wizard will appear as the first master in the second category of the New dialog.
    • category is a category in the list of which the wizard will be placed. The letter indicates the position in the category list in the New dialog.
  4. The following code defines the icon and text that will appear in the New dialog: <icon> console.png </ icon>
    <description> Creates a hello-world-project with custom message. </ description>
    <description xml: lang = "de"> Erzeugt ein Hello-Welt-Projekt mit einer Nachricht. </ description>
    <displayname> Hello World </ displayname>;
    <displayname xml: lang = "de"> Hallo Welt </ displayname>;
    <displaycategory> Custom Projects </ displaycategory>

    <displaycategory xml: lang = "de"> Benutzerdefinierte Projekte </ displaycategory>

    • An icon will appear next to displayName.
    • description appears at the bottom of the New dialog when the project name is highlighted.
    • displayName appears in the New dialog under displayCategory.
      For text elements you can add translations. Specify the target language as an attribute of the element. Use locale names (QLocale). For example, xml: lang = "de".
  5. The following code describes the files being added to the project:
    <files>
    <file source = "main.cpp" openeditor = "true" />
    <file source = "project.pro" target = "% ProjectName% .pro" openproject = "true" />
    • source specifies the file to copy to the project. Files must be located in the wizard directory.
    • target describes the new file name. The% ProjectName% variable is replaced with the string that the user enters in the Name field on the first page of the wizard.
    • openproject indicates that the file is a project file and should be open after the completion of the wizard.
    • openeditor indicates that the file should be opened in the editor after the completion of the wizard.
  6. The following code creates a page that clarifies project settings:
    <! - Create a wizard with parameters ->
    <fieldpagetitle> Hello World Parameters </ fieldpagetitle>
    <fieldpagetitle xml: lang = "de"> Hallo Welt Parameter </ fieldpagetitle>
    <fields>
    <field mandatory = "true" name = "MESSAGE">
    <fieldcontrol class = "QLineEdit" validator = '^ [^ "] + $' defaulttext =" Hello world! "/>
    <fielddescription> Hello world message: </ fielddescription>
    <fielddescription xml: lang = "de"> Hallo-Welt-Nachricht: </ fielddescription>
    </ field>
    </ fields>
    • fieldpagetitle specifies the page title.
    • field indicates whether a field is required (true or false). You can use the value of the name field as a variable in other files (for example,% MESSAGE%).
    • fieldcontrol indicates a field. class indicates the type of field. To create fields, you can use the QWidget class heirs. This example uses QLineEdit to create an input field.
    • validator specifies a regular expression to check for valid characters in the field.
    • defaulttext specifies the text that will appear in the default field.
    • fielddescription specifies the name of the field that will appear on the wizard page.

Creating Class Masters

The widget.xml file for the class wizard is very similar to the file for the project wizard. The differences are described below.

To create a class wizard:

  1. The following code snippet describes the wizard settings:
    <wizard version = "1" kind = "class" id = "A.ListModel" category = "B.CustomClasses">

    <description> Creates a QAbstractListModel implementation. </ description>

    <description xml: lang = "de"> Erzeugt eine Implementierung von QAbstractListModel. </ description>

    <displayname> QAbstractListModel implementation </ displayname>

    <displayname xml: lang = "de"> Implementierung von QAbstractListModel </ displayname>

    <displaycategory> Custom Classes </ displaycategory>

    <displaycategory xml: lang = "de"> Benutzerdefinierte Klassen </ displaycategory>

    For more information about elements and their meanings, see Creating Project Wizards.

  2. The following code describes the files being added to the project:
    <files>

    <file source = "listmodel.cpp" target = "% ClassName: l%.% CppSourceSuffix%" openeditor = "true" />

    <file source = "listmodel.h" target = "% ClassName: l%.% CppHeaderSuffix%" openeditor = "true" />

    </ files>

    Here target contains the following variables that are used to generate the file name:

    • % ClassName: l% is replaced by the value of the ClassName field. The l modifier translates the string to lower case to enforce the Qt conventions.
    • % CppSourceSuffix% is replaced with the default source file suffix, which is defined in Qt Creator in Tools> Options ...> C ++> File Naming . For example, if a user enters MyClass , when creating a project, the file name will be myclass.cpp.
    • % CppHeaderSuffix% is replaced with the default header file suffix, which is also defined in File naming . In this case, the file name will be myclass.h.
  3. The following code creates a page that allows users to select the name of the class, base class and header file and source file for the class:
    <! - Create parameter wizard page ->

    <fieldpagetitle> ListModel parameters </ fieldpagetitle>

    <fieldpagetitle xml: lang = "de"> Parameter des ListModel </ fieldpagetitle>

    <fields>

    <field name = "ClassName">

    <fieldcontrol class = "QLineEdit" validator = "^ [a-zA-Z0-9 _] + $" defaulttext = "MyListModel" />

    <fielddescription> Class name: </ fielddescription>

    <fielddescription xml: lang = "de"> Klassenname: </ fielddescription>

    </ field>

    <field name = "Datatype">

    <fieldcontrol class = "QComboBox" combochoices = "QString, int" defaultindex = "0" />

    <fielddescription> Data type: </ fielddescription>

    <fielddescription xml: lang = "de"> Datentyp: </ fielddescription>

    </ field>

    </ fields>

    In addition to QLineEdit, QComboBox was used to create the field in the class wizard. combochoices defines the options in the drop-down list, and defaultindex indicates which QString element is the default value.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Cross platform programming

Terms: Cross platform programming