aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmljsscope/data/functionAndBindingIndices.qml
blob: 5a8442d6f5bfa0d895739cf2e6613ed0839a87d4 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import QtQml
import QtQuick
import QQmlJSScopeTests

Text {
    id: root

    // js func
    function jsFunc() { return true; }

    // js func with typed params
    function jsFuncTyped(url: string) { return url + "/"; }

    // script binding (one-line and multi-line)
    elide: Text.ElideLeft
    color: {
        if (root.truncated) return "red";
        else return "blue";
    }

    property int newProperty: {
        var callable = () => { return 42; };
        return callable();
    }

    // group prop script binding (value type and non value type)
    font.pixelSize: (40 + 2) / 2
    anchors.bottomMargin: 11 + 1
    anchors.topMargin: {
        var divideBy4 = function (x) { return x / 4; };
        return divideBy4(44);
    }

    // attached prop script binding
    Keys.enabled: root.truncated ? true : false

    // property change handler ({} and function() {} syntaxes)
    property int p0: 42
    property Item p1
    property string p2
    onP0Changed: console.log("p0 changed");
    onP1Changed: {
        console.log("p1 changed");
    }
    onP2Changed: function() {
        console.log("p2 changed:");
        console.log(p2);
    }

    // signal handler ({} and function() {} syntaxes)
    signal mySignal0(int x)
    signal mySignal1(string x)
    signal mySignal2(bool x)
    onMySignal0: console.log("single line", x);
    onMySignal1: {
        var identity = (x) => { return x; };
        console.log("mySignal1 emitted:", identity(x));
    }
    onMySignal2: function (x) {
        var returnString = function() { return "mySignal2 emitted:"; };
        console.log(returnString(), x);
    }

    // var property assigned a js function
    property var funcHolder: function(x, y) { return x + y; }

    component InlineType : Item {
        function jsFuncInsideInline() { return 42; }
        objectName: "inline" + " " + "component"
        Item { // inside inline component
            y: 40 / 2
        }
    }
    InlineType {
        function jsFuncInsideInlineObject(x: real) { console.log(x); }
        Item { // outside inline component
            focus: root.jsFunc();
        }
    }

    TableView {
        delegate: Text {
            signal delegateSignal()
            onDelegateSignal: { root.jsFunc(); }
        }

        component InternalInlineType : Rectangle {
            function jsFuncInsideInline2() { return 43; }
        }

        property var funcHolder2
        funcHolder2: function(x) { return x * 2; }
    }

    ComponentType {
        property string foo: "something"
        onFooChanged: console.log("foo changed!");
    }

    GridView {
        delegate: ComponentType {
            function jsFuncInsideDelegate(flag: bool) { return flag ? "true" : "false"; }
        }
    }

    property list<Item> itemList: [
        Text {
            function jsInsideArrayScope() { return 42; }
        },
        Item {
            property var x123: function(a) { return a + 77; }
            function jsInsideArrayScope2(flag) {
                var closure = () => { return "foobar"; };
                if (flag)
                    return closure;
                return () => { return "bazbar"; };
            }
        },
        Rectangle {} // dummy
    ]

    // translations:
    property string translated1: qsTr("bad but ok")
    property string translated2: qsTrId("bad_but_ok_id")
    property string translated3: qsTr("bad but ok") + "?"
    property string translated4: qsTrId("bad_but_ok_id") + "?"

    property string translated5
    property string translated6
    translated5: qsTr("bad but ok")
    translated6: qsTrId("bad_but_ok_id")

    // function with nested one
    function jsFunctionReturningFunctions() {
        return [ function() { return 42 }, () => { return "42" } ];
    }

    // function with Qt.binding()
    function bindText() {
        root.text = Qt.binding( function() { return jsFuncTyped("foo") + "bar"; } );
    }

    // special code which fails somehow:
    TypeWithProperties {
        onAChanged: console.log("x");
    }

    TypeWithProperties {
        onAChanged: function() { }
    }
}