Qt Designer supports two approaches to creating template forms. The simplest approach involves little more than saving a .ui file into the templates directory. The second approach involves creating a container widget class to be used as a base class for forms that use the template. We will explain both techniques.
These templates are most useful when you want to create a whole set of forms which all have some common widgets. For example, you might have a project that will require many forms, all of which need to be branded with a company name and logo.
First we'll create the simple template.
Click File|New to invoke the New Form dialog. Click the Dialog template then click OK.
Click the Text Label toolbar button, then click near the top left of the form. Change the font Point Size property to 16 and change the text property to your or your company's name. Click the Line toolbar button, then click the form below the label; click Horizontal on the pop-up menu.
Select the label and the line. ( Ctrl+Click the form, then drag the rubber band so that it touches or includes the line and the label.) Press Ctrl+L to lay them out vertically.
Click the Save toolbar button. In the Save As dialog, navigate to Qt Designer's templates directory, e.g. (qt/tools/designer/templates. Type in the name 'Simple_Dialog.ui' and click Save.
Right click the form in the Forms list, then click Remove form from project.
Now that we have the simple template we are ready to use it. Click File|New to invoke the New Form dialog. One of the templates that will appear is 'Simple Dialog'. Click the simple dialog, then click OK. A new form will appear with the same widgets and layout as the template. Add any other widgets and functionality. When you attempt to save the form you will be prompted for a new form name.
These templates are useful when you want to provide some default functionality that all the forms based on the base class can inherit. In our example we'll use a class called SizeAware that remembers and restores its size as the basis of a template. We won't describe the class itself, but will focus instead on making use of it as a Qt Designer template. The source for the class is in qt/tools/designer/examples/sizeaware.
The template can either be based on a custom widget or on any existing container widget.
If you want to base the template on a custom widget you must first add it to Qt Designer's custom widgets. Click Tools|Custom|Edit Custom Widgets to invoke the Edit Custom Widgets dialog. (This dialog is explained in more detail in the section called Simple Custom Widgets in Chapter 5 "Simple Custom Widgets".) Click New Widget. Change the Class from 'MyCustomWidget' to 'SizeAware'. Click the Headerfile ellipsis button and select the file qt/tools/designer/examples/sizeaware/sizeaware.h. Check the Container Widget checkbox. This class provides two properties. Click the Properties tab. Click New Property and change the property name to 'company'. Click the New Property again and change the property name to 'settingsFile'. Click Close.
To create a template, based on an existing widget or on your own custom widget, click File|Create Template to invoke the Create Template dialog. Change the Template Name to 'SizeAware' and click the SizeAware base class, then click Create. The dialog will create the template and close itself immediately. Close Qt Designer and restart it.
A new template, 'SizeAware' is now available from the list of templates. Click File|New, click SizeAware and click OK. Note that the two properties, company and settingsFile, are available in the Properties window. Any forms based on this template will remember their size and resize when reloaded. (In practical applications having one settingsFile per form is not recommended, so this template would only really be useful for applications that have a single main window.)