summaryrefslogtreecommitdiffstats
path: root/howtos/button/index.rst
blob: 1944ee5899a25744cf0dc8990f70090ed090267d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
..
    ---------------------------------------------------------------------------
    Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
    All rights reserved.
    This work, unless otherwise expressly stated, is licensed under a
    Creative Commons Attribution-ShareAlike 2.5.
    The full license document is available from
    http://creativecommons.org/licenses/by-sa/2.5/legalcode .
    ---------------------------------------------------------------------------

How to make a simple button
===========================

A button is basically a rectangle which you can press.

.. code-block:: js

    // Button1.qml
    import QtQuick 1.1

    Rectangle {
        id: root
        width: 64; height: 32
        color: "red"

        signal clicked()

        MouseArea {
            anchors.fill: parent
            onClicked: root.clicked()
        }
    }

This is how you use the button:

.. code-block:: js

    import QtQuick 1.1

    // main.qml
    Rectangle {
        width: 300; height: 300
        color: "black"

        Button1 {
            anchors.centerIn: parent
            onClicked: print("Button Pressed")
        }
    }

This is indeed a very simple button. You could add a ``Text`` element centered on the button to make it even more useful, e.g. something like this:

.. code-block:: js

    import QtQuick 1.1

    Rectangle {
        id: root
        property string label: "no label"
        width: labelText.width + 10
        height: labelText.height + 10
        color: "red"
        radius: 8

        signal clicked()

        Text {
            id: labelText
            text: label
            anchors.centerIn: parent
        }

        MouseArea {
            anchors.fill: parent
            onClicked: root.clicked()
        }
    }