Qt Creator Manual

Creating Components

A component provides a way of defining a new type that you can re-use in other QML files. A component is like a black box; it interacts with the outside world through properties, signals, and slots, and is generally defined in its own QML file. You can import components to screens and applications.

You can use the following QML elements to create components:

  • Border Image uses an image as a border or background.
  • Image adds a bitmap to the scene. You can stretch and tile images.
  • Item is the most basic of all visual items in QML. Even though it has no visual appearance, it defines all the properties that are common across visual items, such as the x and y position, width and height, anchoring, and key handling.
  • Rectangle adds a rectangle that is painted with a solid fill color and an optional border. You can also use the radius property to create rounded rectangles.
  • Text adds formatted read-only text.
  • Text Edit adds a single line of editable formatted text that can be validated.
  • Text Input adds a single line of editable plain text that can be validated.
  • Web View adds web content to a canvas.

QML elements allow you to write cross-platform applications with custom look and feel. You can also use ready-made Qt Quick Components that enable you to create applications with a native look and feel for a particular target platform. You can install the components as part of the Qt 4 SDK.

When you use the Qt Creator project wizard to create Qt Quick applications, you can select which component set to use in your application.

Even if you use the Qt Quick Components, you can still write cross-platform applications, by using different sets of QML files for each platform.

Creating Components in Qt Quick Designer

  1. Select File > New File or Project > Files and Classes > QML > Choose to create a new .qml file.

    Note: Components are listed in the QML Components section of the Library pane only if the filename begins with a capital letter.

  2. Click Design to open the .qml file in Qt Quick Designer.
  3. Drag and drop an item from the Library pane to the editor.
  4. Edit item properties in the Properties pane.

    The available properties depend on the item.

The following sections contain examples of how to create some common components:

Moving Within Components

Components can consist of several other components. To view the component hierarchy as a bread crumb path when you edit a component on the canvas, select Go into Component or press F2. Click the component names in the path to navigate to them. You can easily navigate back to the top level when you are done editing the component.

"Go into Component command"