Creating Scalable Buttons and Borders
You can use the Border Image element to display an image, such as a PNG file, as a border and a background.
Use two Border Image elements and suitable graphics to make it look like the button is pushed down when it is clicked. One of the Border Image elements is visible by default. You can specify that it is hidden and the other one becomes visible when the mouse is clicked.
Add a MouseArea 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.
To create a graphical button:
- Select File > New File or Project > QML > QML File > Choose... to create a QML file called Button.qml (for example).
- Double-click the file to open it in the code editor.
- Replace the Rectangle with an Item, as illustrated by the following code snippet:
Item {
}
- Specify properties and set expressions for the Item, as illustrated by the following code snippet:
property string text: ""
property int fontSize: 44
signal clicked
You will point to the properties and expression later.
- Click Design to edit the file in the visual editor.
- Drag and drop two BorderImage items from the Library pane to the scene.
- Drag and drop a Text item to the scene.
- Drag and drop a MouseArea to the screen.
- In the Navigator pane, select border_image1 to specify settings for it in the Properties pane:
- Select Set Expression in the menu next to the Visibility check box.
- Enter the following expression to specify that the image is visible when the mouse is not pressed down: !mouse_area1.pressed.
- In the Source field, select the image file for the button, for example button_up.png.
- Click Layout, and then click the button to anchor the border image to the Item.
- Select border_image2 to specify similar settings for it:
- Set the following epression for Visibility, to specify that the image is visible when the mouse is pressed down: mouse_area1.pressed.
- In the Source field, select the image file for the button when it is clicked, for example button_down.png.
- Click Layout, and then click the button to anchor the border image to the Item.
- Select text1 to specify font size and color, and text scaling and rendering:
- In the Color field, use the color picker to select the font color, or enter a value in the field.
- In the Text field, select Set Expression and enter a pointer to the text property that you specified earlier: parent.txt.
- Select the Aliasing check box to enable smooth text rendering.
- In the Size field, select Pixels to specify the font size in pixels. By default, the size is specified in points.
- In the Size field, select Set Expression and enter a pointer to the fontSize property that you specified earlier.
- Click Layout, and then click the buttons to inherit the vertical and horizontal centering from the parent.
- Click Advanced to specify scaling for the text in the Scale field.
- Select Set Expression 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.
- In the code editor, add to the MouseArea item a pointer to the clicked expression that you added earlier: onClicked: parent.clicked().
Note: To view the button, you must add it to a Qt Quick Application or Qt Quick UI project.
|