Log In UI - Part 2

Illustrates how to position UI components on pages using anchors and positioners.

"Log In UI"

Log In UI - Part 2 is the second in a series of tutorials that build on each other to illustrate how to use Qt Design Studio to create a simple UI with some basic UI components, such as pages, buttons, and entry fields. Part 2 describes how to position the UI components on pages to create a scalable UI.

To ensure that the layout is responsive and all the UI components stay in their proper places when you resize the UI on the desktop or on devices with different screen sizes, you will use anchors and positioners.

These instructions build on Log In UI - Part 1.

The Learn More sections provide additional information about the task at hand.

Anchoring UI Components

First, you will anchor the static page elements, logo image (logo) and page title (pageTitle), to the page. When you created the project using the new project wizard template in Part 1 of this tutorial, the wizard template anchored pageTitle to the vertical and horizontal center of the page. Therefore, you will only need to replace the vertical anchor of pageTitle with a top anchor to align it with logo on the page.

To preview the changes that you make to the UI while you make them, select the (Show Live Preview) button on the Form Editor toolbar or press Alt+P.

To anchor component instances on a page:

  1. Open Screen01.ui.qml for editing in the Form Editor view.
  2. Select logo in the Navigator view.
  3. In the Properties view, Layout tab, select the (Top) and (Left) anchor buttons to anchor logo to the top left corner of its parent with 10-pixel margins.
  4. Select pageTitle in Navigator.
  5. Deselect the (Vertical Center) button to remove the anchor, and then select the Top button to anchor the title to the top of the page with a 70-pixel margin.
  6. Select File > Save or press Ctrl+S to save your changes.

Your page now should look something like this in the Design mode and live preview:

"Login page in the Design mode"

Learn More - Anchors

In an anchor-based layout, each component instance can be thought of as having a set of invisible anchor lines: top, bottom, left, right, fill, horizontal center, vertical center, and baseline.

Anchors enable placing a component instance either adjacent to or inside of another component instance, by attaching one or more of the instance's anchor lines to the anchor lines of the other component instance. If a component instance changes, the instances that are anchored to it will adjust automatically to maintain the anchoring.

For more information, see Positioning with Anchors.

Using Column Positioners

You will now add two entry fields to the page and position them in a column to learn another method of positioning components. Next, you will position the push buttons on the page in another column. Finally, you will anchor the columns to the page to enable their positions to change when the screen size changes.

To add entry fields to the page:

  1. Drag-and-drop two instances of the Text Field component from Library > Components > Qt Quick Controls to Form Editor.
  2. Select one of the text fields in Navigator, and change its ID to usernameField in Properties.
  3. In the Geometry group, Size field, make the field wide enough to accommodate long user names by setting its width to 300 pixels.
  4. In the Placeholder field, set the text to display inside the field before users type in it to Username
  5. Select tr to mark the text translatable.

    "Text field properties"

  6. Select the other text field, and change its ID to passwordField, placeholder text to Password, and width to 300 pixels.

The Text Field component has additional properties that you can use to change its appearance. For example, you can change the color, font, alignment, or word and letter spacing of the placeholder text.

You will now position the fields and buttons as columns:

  1. Select usernameField and passwordField in Navigator (press the Shift key for multiple selection), and right-click either of them to open a context menu.
  2. Select Position > Position in Column to position the fields on top of each other in Form Editor.
  3. Select the column in Navigator and change its ID to fieldColumn in Properties.
  4. In the Spacing field, set the spacing between the fields to 5 pixels.

    "Column properties"

  5. Select loginButton and registerButton, and then select Position > Position in Column to position them in a column.
  6. Select the button column, change its ID to buttonColumn, and set the spacing between the buttons to 5 pixels, as above.

You will now anchor the field and button columns to the page:

  1. Select fieldColumn in Navigator.
  2. In Properties > Layout, select the Top button to anchor the top of the button column to the top of its parent with a 200-pixel margin.
  3. Select the (Horizontal Center) button to center the field column horizontally on the page.
  4. Select buttonColumn in Navigator.
  5. In Properties > Layout, select the (Bottom) button to anchor the bottom of the button column to the bottom of its parent with a 50-pixel margin.
  6. Select the Horizontal Center button to center the button column horizontally on the page.
  7. Select File > Save or press Ctrl+S to save your changes.

The second iteration of your UI is now ready and should look something like this in the Design mode and live preview:

"Login page in the Design mode"

Learn More - Positioners

For many use cases, the best positioner to use is a simple grid, row, or column, and Qt Design Studio provides components that will position children in these formations in the most efficient manner possible. For more information about using preset positioners, see Using Positioners.

For more complicated UI designs, you can use components from the Qt Quick Layouts module.

Next Steps

To learn how to add a second page and move to it from the main page, see the next tutorial in the series, Log In UI - Part 3.



Available under certain Qt licenses.
Find out more.