Form layouts

Easily create attractive interaction areas with Kirigami FormLayouts

Kirigami.FormLayoutarrow-up-right components make it easy for you to create forms that conform to the KDE Human Interface Guidelinesarrow-up-right. They are optimal for settings dialogs and for large groups of controls and input fields that are related to each other.

When provided with enough space, form layouts will take up two columns. The column on the left will be occupied by the labels provided for the form's children components, while the right will be taken up by the children components themselves. In more space-constrained windows (or on mobile), forms will consist of a single vertical column with the labels of children components being placed above their respective component.

Simple form

Kirigami.FormLayoutarrow-up-right components are similar in use to QtQuick Layoutarrow-up-right components such as ColumnLayoutarrow-up-right or RowLayoutarrow-up-right. The child components will be automatically arranged according to the size available to the form layout.

Children of a Kirigami.FormLayoutarrow-up-right have a property named Kirigami.FormData.labelarrow-up-right. This property lets you set the label that will be provided for the child component in question.

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls as Controls
import org.kde.kirigami as Kirigami

Kirigami.ApplicationWindow {
    pageStack.initialPage: Kirigami.Page {

        Kirigami.FormLayout {
            anchors.fill: parent

            Controls.TextField {
                Kirigami.FormData.label: "TextField 1:"
            }
            Controls.TextField {
                Kirigami.FormData.label: "TextField 2:"
            }
            Controls.TextField {
                Kirigami.FormData.label: "TextField 3:"
            }
        }
    }
}
A simple form layout in desktop mode

Sections and separators

FormLayouts can also be divided into sections. Setting where a section starts is as easy as setting a child component's Kirigami.FormData.isSectionarrow-up-right to true. This will provide the component with some extra margin at the top to demarcate the start of the new section.

Kirigami.Separatorarrow-up-right components are best suited for starting new sections. Separators are used to draw a thin horizontal line, demarcating the end of a section. If you would rather not have a line drawn between sections, you can use a standard QML Itemarrow-up-right property. Alternatively you could use the Kirigami.FormData.isSectionarrow-up-right property on any other component.

However, this is not recommended. On components where Kirigami.FormData.isSectionarrow-up-right is set to true, the label text provided for this component's Kirigami.FormData.labelarrow-up-right property will be displayed as the section's header text.

circle-exclamation

This header text is larger than the normal label text, and provides users with a nice visual cue of what the form layout section is about.

A form layout with sections

Checkable children

A handy feature of Kirigami.FormLayoutarrow-up-right is that you can add checkboxes to its children. This can be useful in settings pages where you might want to let the user enable or disable a setting, and also want the user to provide some extra information in a component such as a textfield.

A form layout with checkable label

Forcing a desktop or mobile layout

If you would rather have your form layout stay consistent regardless of your application's environment, you can use the wideModearrow-up-right property of the Kirigami.FormLayoutarrow-up-right component:

  • When set to true, the form layout will be structured in a desktop-optimized widescreen (double-column) layout

  • When set to false, the form layout will be structured in a mobile layout (single column)

A form layout with forced mobile layout

Aligning your labels

There are instances when you want a label to be assigned to components that have more than one line or to a list of components. This can be achieved by putting the Kirigami.FormData.labelarrow-up-right in the ColumnLayoutarrow-up-right, as you might have noticed in Sections and Separators. By default the label is positioned in the vertical center of the layout, which is not always desirable. We can change this with help of Kirigami.FormData.labelAlignmentarrow-up-right.

A form layout with top-aligned label

Setting the label alignment is particularly convenient to manage components or lists of components whose size you do not know beforehand. Elisaarrow-up-right is a very good example of this:

The Comment label is only top aligned when its corresponding component has more than one line

We can do something similar to this with a JavaScript ternary operator:

Last updated