Log In UI - Positioning

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

"Log In UI"

Log In UI - Positioning 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. The second tutorial in the series 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.

The starting point for this tutorial is the completed Log In UI - Components project. You can download the project here.

Additionally, you can download the completed project of this tutorial here.

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

Anchoring UI Components

First, you will anchor the static page elements, background image (adventurePage), logo (qt_logo_green_128x128px), and tag line (tagLine), to the page.

To preview the changes that you make, select the Live Preview button or press Alt+P.

To anchor component instances on a page:

  1. Open Screen01.ui.qml for editing in the 2D view.
  2. Select adventurePage in the Navigator view to display its properties in the Properties view.
  3. In Properties > Layout, select the anchor button to anchor adventurePage to its parent in the Target field. This attaches the background image to the rectangle on all sides.

    Note: Selecting the anchor button should automatically select the four buttons on the left side of it. If it doesn't, select on the 2D view toolbar to refresh the 2D view.

    "Layout properties"

  4. Select qt_logo_green_128x128px in Navigator.
  5. In Properties > Layout, set:
    • Anchors to and .
    • Target to parent to anchor the logo to its parent.
    • Margin to 40 and select to anchor the logo to the top of the target.

    This attaches the logo to the rectangle at the top while keeping its horizontal center aligned with that of the rectangle.

    "Logo Layout properties"

  6. Select tagLine in Navigator.
  7. In Properties > Layout, set:
    • Anchors to and .
    • Target to qt_logo_green_128x128px to anchor the tag line to the logo.
    • Margin to 40 and select to anchor the tag line to the bottom of the target.

    This attaches the tag line to the bottom of the logo while keeping its horizontal center aligned with that of the rectangle.

    "Text Layout properties"

    Note: You can anchor component instances to their parent and sibling components. If a component instance is not listed in the Target field, check that the component instance is located in the correct place in the component hierarchy in Navigator. For more information, see Arranging Components.

  8. Select File > Save or press Ctrl+S to save your changes.

Your page should now look something like this in the 2D view and Live Preview:

"Login page in the 2D view and live preview"

Using Column Positioners

You will now position the entry fields and buttons in columns to learn another method of positioning components. Then, you will anchor the columns to the page to enable their positions to change when the screen size changes.

To position the fields and buttons as columns:

  1. Select username and password in Navigator (press and hold the Shift or Ctrl key for multiple selection), and right-click either of them to open a context menu.
  2. Select Positioner > Column Positioner to position the fields on top of each other in the 2D view.
  3. Select the column in Navigator and change its ID to fields in Properties.
  4. In Column > Spacing, set the spacing between the fields to 20.

    "Column properties"

  5. Select login and createAccount, and then select Positioner > Column Positioner to position them in a column.
  6. Select the button column, change its ID to buttons, and set the spacing between the buttons to 20, as above.

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

  1. Select fields in Navigator.
  2. In Properties > Layout, set:
    • Anchors to and .
    • Target to tagLine to anchor the entry fields to the tag line.
    • Margin to 170 and select to anchor the entry fields to the bottom of the target.

    This attaches the entry fields to the bottom of the tag line while keeping its horizontal center aligned with that of the rectangle.

    "Properties of fields"

  3. Select buttons in Navigator.
  4. In Properties > Layout, set:
    • Anchors to and .
    • Target to parent to anchor the entry fields to the rectangle.
    • Margin to 50 and select to anchor the entry fields to the bottom of the target.

    This attaches the buttons to the bottom of the rectangle while keeping its horizontal center aligned with that of the rectangle.

    "Properties of buttons"

  5. 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 2D view and Live Preview:

"Login page in the 2D view and live preview"

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.

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 - States.

Files:

Available under certain Qt licenses.
Find out more.