Creating Scalable Buttons and Borders

You can use the BorderImage type to display an image, such as a PNG file, as a border and a background.

Use two Border Image items and suitable graphics to make it look like the button is pushed down when it is clicked. One of the Border Image items is visible by default. You can specify that it is hidden and the other one becomes visible when the mouse is clicked.

Add a Mouse Area that covers the whole area and emits the clicked signal (parent.clicked()) when it detects a mouse click.

You can add text to the button and set it up as a property. The text can then be initialized from the outside, making the button a reusable UI component. The font size is also available in case the default size is too big. You can scale down the button text and use smooth text rendering for some extra quality.

"Graphical button"

To create a graphical button:

  1. Select File > New File or Project > Qt > QML File > Choose to create a QML file called Button.qml (for example).
  2. Double-click the file to open it in the code editor.
  3. Replace the Rectangle with an Item, as illustrated by the following code snippet:
    Item {
    
    }
  4. Specify properties and set expressions for the Item, as illustrated by the following code snippet:
    Item {
        property string text: ""
        property int fontSize: 10
    
        signal clicked
    
        width: 60
        height: 40
    }

    You will point to the properties and expression later.

  5. Click Design to edit the file in the visual editor.
  6. Drag and drop two Border Image items from the Library to the canvas.
  7. Drag and drop a Text item to the canvas.
  8. Drag and drop a Mouse Area to the canvas.
  9. In the Navigator, select border_image1 to specify settings for it in the Properties pane:
    1. Select Set Binding in the menu next to the Visibility check box.
    2. Enter the following expression to specify that the image is visible when the mouse is not pressed down: !mouse_area1.pressed.
    3. In the Source field, select the image file for the button, for example button_up.png.
    4. Click Layout, and then click the (Fill to Parent) button to anchor the border image to the Item.
  10. Select border_image2 to specify similar settings for it:
    1. Set the following epression for Visibility, to specify that the image is visible when the mouse is pressed down: mouse_area1.pressed.
    2. In the Source field, select the image file for the button when it is clicked, for example button_down.png.
    3. Click Layout, and then click the Fill to Parent button to anchor the border image to the Item.
  11. Select text1 to specify font size and color, and text scaling and rendering:
    1. In the Color field, use the color picker to select the font color, or enter a value in the field.
    2. In the Text field, select Set Binding and enter a pointer to the text property that you specified earlier: parent.txt.
    3. In the Size field, select Pixels to specify the font size in pixels. By default, the size is specified in points.
    4. In the Size field, select Set Expression and enter a pointer to the fontSize property that you specified earlier.
    5. Click Layout, and then click the "Anchor buttons" (Set Vertical Anchor and Set Horizontal Anchor) buttons to inherit the vertical and horizontal centering from the parent.
    6. Click Advanced to specify scaling for the text in the Scale field.
    7. Select Set Binding and enter the following expression: if (!mousearea1.pressed) { 1 } else { 0.95 }.

      Note: You can enter long and complicated expressions also in the code editor.

  12. In the code editor, add to the MouseArea a pointer to the clicked expression that you added earlier: onClicked: parent.clicked().

Note: To test the button, add it to a Qt Quick Application or Qt Quick UI project and run the application.

© 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.