Log In UI - Part 4

Illustrates how to use the timeline and states to animate UI components.

Log In UI - Part 4 is the fourth in a series of examples 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 4 describes how to use the timeline and states to animate UI components.

In Part 3, you learned how to use states to simulate page changes in a UI and connections to provide user interaction with it. In Part 4, you will now learn another way of animating the UI by using timeline animations that you bind to states.

These instructions build on:

The Learn Qt Quick sections provide additional information about the features of QML and Qt Quick that are relevant to the task at hand.

Animating UI Components

In Part 3, you changed the visibility property in different states to simulate changing pages. To make sure that those changes won't interfere with the changes to the opacity property you will make in Part 4, you will first remove the states.

Then, you will add a timeline and insert keyframes for the opacity property to hide the password verification field and back button on the login page and the login button on the registration page. Because we want the password verification field to appear to slide down from the password field, you will also need to insert a keyframe for its anchor margin property. To be able to animate the anchor, you also need to pull out the fields from the fields column and anchor them to the page and to each other instead.

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.

Replacing Columns with Anchors

First, you will prepare the page for adding animation:

  1. Open Screen01.ui.qml in Form Editor for editing.
  2. In the States view, select the (Close) button in loginState and registerState to remove the states.
  3. Select the fields in fieldColumn in Navigator and drag and drop them to loginPage.
  4. Select fieldColumn in Navigator and press Delete to delete it.
  5. Select usernameField in Navigator.
  6. In Properties > Layout, select the (Top) button to anchor the top of the field to the top of its parent (loginPage). Qt Design Studio will suggest an appropriate margin based on the current Y-position of the field.
  7. Select the (Horizontal Center) button to anchor the horizontal center of the field to that of its parent.
  8. Select passwordField, and then select the Top button in Properties > Layout.
  9. In the Target field, select usernameField to anchor the top of passwordField to the bottom of usernameField with a 5-pixel margin.
  10. Select the Horizontal Center button to anchor the horizontal center of passwordField to that of usernameField.
  11. Repeat the above steps to anchor the top of verifyPasswordField to the bottom of passwordField with a 5-pixel margin and to anchor its horizontal center to that of passwordField.
  12. Select File > Save or press Ctrl+S to save your changes.

You could also animate the Y-position property of the verify password field for a similar effect. In that case, you would need to use absolute positioning for the field. This is less flexible if you export your design from a design tool, such as Adobe Photoshop, and decide to change your design and export it again at some point. In that case, the margins would probably stay the same, even if the positions of the fields on the page would change.

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

"UI with all the buttons and fields in the Design mode"

Adding a Timeline and Animation Settings

You are now ready to add the timeline. You will need two animations, one for moving into the registration page and another for returning to the login page. You can use the same animation for both cases, by running it either from the beginning to the end or from the end to the beginning.

To add a timeline with settings for running the animation:

  1. Select View > Views > Timeline to open the Timeline view.
  2. In Timeline, select to add a 1000-frame timeline and settings for running the animation.

  3. In the Animation ID field, enter toLoginState.
  4. Deselect the Running in base state check box, because you want the animation to run only after the user clicks the Create Account button. You can use the default settings for the other fields.
  5. Select next to the Animation Settings group to add settings for running the animation when the user clicks the back button.
  6. In the Animation ID field, enter toRegisterState.
  7. To run the animation backwards when the user clicks the back button, enter 1000 in the Start frame field and 0 in the End frame field.
  8. Select Close in the Timeline Settings view to save the timeline and the animation settings.

Next, you will record the animation in Timeline.

Inserting Keyframes

You will now insert keyframes and record property changes in Timeline:

  1. Select backButton in Navigator.
  2. In Properties > Opacity > Settings, select Insert Keyframe to insert a keyframe for the opacity property of the button.
  3. Check that the playhead is in frame 0, and select the (Per Property Recording) button for the opacity property of backButton to start recording property changes.

    "Record button for the opacity property"

  4. In the field next to the opacity property name on that same line, type 0 to hide the button, and press Enter to save the value.
  5. Move the playhead to frame 1000 and change the opacity value to 1 to show the button.
  6. Select the record button again to stop recording property changes. If you forget this, all the following changes will be recorded, and the results will be unpredictable.
  7. Select verifyPasswordField in Navigator, and repeat the above steps to insert a keyframe for the opacity property of the field and to record changes for it.
  8. Select loginButton in Navigator, and repeat the above steps to insert a keyframe for the opacity property of the button and to record changes for it. However, this time the opacity value needs to be 1 in frame 0 and 0 in frame 1000.
  9. Select File > Save or press Ctrl+S to save your changes.

When you move the playhead along the timeline, you can see how the login button fades out while the verify password field and back button fade in.

You will now animate the top anchor margin of the verify password field to make it appear to slide down from the password field.

Animating Anchors

To animate the top anchor margin of the verify password field:

  1. Select verifyPasswordField in Navigator.
  2. Select Properties > Layout > Margin > Insert Keyframe to insert a keyframe for the top anchor margin of verifyPasswordField.
  3. In Timeline, check that the playhead is in frame 0, and select the record button for the anchors.topMargin property of verifyPasswordField.
  4. In the field next to the property, set a negative value for the top anchor margin, -40, to place verifyPasswordField on top of passwordField.
  5. Move the playhead to frame 1000 and change the top anchor margin to 5, so that verifyPasswordField appears to slide down and settle below passwordField.
  6. Select the record button again to stop recording property changes.
  7. Select File > Save or press Ctrl+S to save your changes.
Adding Easing Curves

You will now add an easing curve to the anchor margin animation that will make the transition seem smoother:

  1. Click the keyframe marker for the anchors.topMargin property at frame 1000 on the timeline to select it.
  2. Right-click the keyframe marker to open a context menu, and select Edit Easing Curve to add an easing curve to the animation.
  3. In Easing Curve Editor, select easeOutSine.

  4. Select OK to close the editor.

When you attach easing curves to keyframes, the shape of the keyframe marker changes from to .

Your timeline should now look something like this:

"Timeline view with the recorded property changes"

Next, you create states for the login and registration pages and bind them to the animation settings.

Binding Animation to States

You will now bring back the states in the States view and bind them to the animation settings in Timeline:

  1. In States, select Create New State twice to add two states called loginState and registerState. You don't need to make any property changes this time, because you'll bind the states to property animations.
  2. In Timeline, select the (Timeline Settings (S)) button on the toolbar (or press S to open the Timeline Settings dialog.

  3. Double-click the cell in the Timeline column on the loginState row, and select timeline in the list.
  4. Double-click the cell in the Animation column on the loginState row, and select toRegisterState.
  5. Repeat these steps for registerState row, but select toLoginState in the Animation column.
  6. Click Close to save the timeline settings.

In the live preview, you can now click the Create Account button to go to the registration page and the back button to return to the login page.

Learn Qt Quick - Timeline

The Qt Quick Timeline module provides QML types to use timelines and keyframes to animate component properties in UIs. Animating properties enables their values to move through intermediate values instead of immediately changing to the target value.

The Keyframe type specifies the value of a keyframe on a timeline. Qt Design Studio automatically adds keyframes between two keyframes, and sets their values evenly to create an appearance of movement or transformation.

An easing curve can be attached to the keyframe to change the appearance of the animation. For more information about easing curve types, see the documentation for easing curves.

To be able to use the functionality of Qt Quick Timeline types, Qt Design Studio adds the following import statement to the QML files where it uses the types:

import QtQuick.Timeline 1.0

All the properties and functions of the QML types from this module are available in the Design mode, and therefore it is enough to learn how to use Timeline, as described in Creating Animations.

Next Steps

For more examples about using timelines, see Examples and Tutorials.

To watch a video tutorial about creating timelines and adding keyframes, select Learn to use Qt Design Studio Part 2 in the Tutorials tab in the Welcome mode.

Files:

Images:

Available under certain Qt licenses.
Find out more.