- Qt Creator Manual
- Creating Custom Components
- Qt Creator Manual 5.0.3

Creating Component Instances ◦ Creating Buttons

# Creating Custom Components

You can either use project wizard templates to create custom components and controls or move component instances into separate files to turn them into new components that you can create instances of. You can then use the instances of the new components in other components.

Custom components are stored in Library > Components > My Components. You can create instances of custom components by dragging-and-dropping them from Library to Form Editor, 3D Editor, or Navigator.

## Creating Components from Scratch

To use wizard templates to create custom components:

1. Select File > New File or Project > Qt > Qt Quick UI File > Choose to create a new `.ui.qml` file.

   Note: Components are listed in Library > Components > My Components only if the filename begins with a capital letter.

2. Click Design to open the file in Form Editor.
3. Drag-and-drop a component from Library to Navigator or Form Editor.
4. Edit component properties in the Properties view. The available properties depend on the component type. You can add properties for components in Connection View > Properties.
5. To change the appearance and behavior of the component instances in ways that are not supported in Qt Design Studio by default, you can define custom properties in Connection View > Properties.

The following sections contain more information about how to use Form Editor to edit 2D content and 3D Editor to edit 3D scenes, as well as examples of how to create UI controls using instances of basic components:

- Form Editor
- 3D Editor
- Creating Buttons
- Creating Scalable Buttons and Borders

## Naming Conventions

Establish naming conventions to keep the components in your UI organized. Name your components accurately and give them suitable IDs. Particularly, check and modify the default names of the components you export from design tools to create reliable and self-explanatory names that follow the naming conventions you have established.

For example, a button symbol might have the ID myButton_symbol and the different layers might have it as a prefix with the states appended, thus producing IDs such as myButton_symbol_default. To prepare for additional mouse areas later in the project, you could use similar IDs for them, such as myButton_symbol_hotspot. When you use the button as instances in a screen, you would give each of these instances a unique ID that relates to that screen. For example, myButton_myMenu_home, myButton_myMenu_profile, and myButton_myMenu_settings

## Turning Component Instances into Custom Components

An alternative way of creating reusable components is to turn component instances into custom components by moving them into separate component files (.ui.qml). Right-click a component instance in Navigator or Form Editor, and select Move Component into Separate File in the context menu.

Give the new component a name, and select whether properties are set for the new component or for the original one.

When you select OK, a new component file is created and an instance of the component is added to the code in the current component file. The look of your UI in Form Editor does not change.

To open the new component file for editing the properties that you want to change for all instances of the component, right-click the component, and then select Go into Component in the context menu. For additional ways of opening base components, see Moving Within Components.

For an example of creating a reusable custom component, see Creating a Mobile Application. Custom components are listed in Library > Components > My Components, and you can use instances of them to build more components.

## Creating UIs for MCUs

Qt for MCUs enables you to use subsets of components to create UIs for devices that are powered by microcontroller units (MCU). The subset of supported components depends on the Qt for MCUs version that you use for development. In this manual, we indicate which components are supported at the time of writing.

To develop for MCUs, create an MCU project. Only the components available on MCUs are displayed in Library > Components. Only a subset of properties is supported for the supported components. The properties that are not available on MCUs are marked in the Properties view with strikethrough text.

For more information about the supported components and their properties, see Qt for MCUs - All QML Types.

## Merging Files with Templates

You can merge the current component file against an existing second component file using the second file in a way similar to using a CSS stylesheet.

To use this experimental feature, right-click a component in the Navigator or Form Editor view and select Merge File with Template in the context menu.

In the Template field, select the file to use as a template.

Creating Component Instances ◦ Creating Buttons