Skip to content

Commit 91d0a93

Browse files
committed
Fix qml.Changed with pointer fields.
1 parent 05a7327 commit 91d0a93

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

bridge.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ func Changed(value, fieldAddr interface{}) {
122122
for valuev.Kind() == reflect.Ptr {
123123
valuev = valuev.Elem()
124124
}
125-
for fieldv.Kind() == reflect.Ptr {
126-
fieldv = fieldv.Elem()
125+
if fieldv.Kind() != reflect.Ptr {
126+
panic("qml.Changed received non-address value as fieldAddr")
127127
}
128+
fieldv = fieldv.Elem()
128129
if fieldv.Type().Size() == 0 {
129130
panic("cannot report changes on zero-sized fields")
130131
}

qml_test.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,21 @@ func (r *GoRect) Paint(p *qml.Painter) {
101101
type GoType struct {
102102
private bool // Besides being private, also adds a gap in the reflect field index.
103103

104-
StringValue string
105-
BoolValue bool
106-
IntValue int
107-
Int64Value int64
108-
Int32Value int32
109-
Uint32Value uint32
110-
Float64Value float64
111-
Float32Value float32
112-
AnyValue interface{}
113-
ObjectValue qml.Object
114-
ColorValue color.RGBA
115-
IntsValue []int
116-
ObjectsValue []qml.Object
117-
MapValue map[string]interface{}
104+
StringValue string
105+
StringAddrValue *string
106+
BoolValue bool
107+
IntValue int
108+
Int64Value int64
109+
Int32Value int32
110+
Uint32Value uint32
111+
Float64Value float64
112+
Float32Value float32
113+
AnyValue interface{}
114+
ObjectValue qml.Object
115+
ColorValue color.RGBA
116+
IntsValue []int
117+
ObjectsValue []qml.Object
118+
MapValue map[string]interface{}
118119

119120
SetterStringValue string
120121
SetterObjectsValue []qml.Object
@@ -715,16 +716,20 @@ var tests = []struct {
715716
GoType {
716717
stringValue: "<old>"
717718
onStringValueChanged: console.log("String is", stringValue)
719+
onStringAddrValueChanged: console.log("String at addr is", stringAddrValue)
718720
}
719721
`,
720722
QMLLog: "!String is",
721723
Done: func(c *TestData) {
722724
c.Assert(c.createdValue, HasLen, 1)
723725
value := c.createdValue[0]
726+
s := "<new at addr>"
724727
value.StringValue = "<new>"
728+
value.StringAddrValue = &s
725729
qml.Changed(value, &value.StringValue)
730+
qml.Changed(value, &value.StringAddrValue)
726731
},
727-
DoneLog: "String is <new>",
732+
DoneLog: "String is <new>.*String at addr is <new at addr>",
728733
},
729734
{
730735
Summary: "qml.Changed must not trigger on the wrong field",
@@ -1216,6 +1221,7 @@ func (s *S) TestTable(c *C) {
12161221

12171222
if !reflect.DeepEqual(t.QMLValue, GoType{}) {
12181223
c.Check(value.StringValue, Equals, t.QMLValue.StringValue)
1224+
c.Check(value.StringAddrValue, Equals, t.QMLValue.StringAddrValue)
12191225
c.Check(value.BoolValue, Equals, t.QMLValue.BoolValue)
12201226
c.Check(value.IntValue, Equals, t.QMLValue.IntValue)
12211227
c.Check(value.Int64Value, Equals, t.QMLValue.Int64Value)
@@ -1245,6 +1251,7 @@ func (s *S) TestTable(c *C) {
12451251

12461252
if !reflect.DeepEqual(t.DoneValue, GoType{}) {
12471253
c.Check(value.StringValue, Equals, t.DoneValue.StringValue)
1254+
c.Check(value.StringAddrValue, Equals, t.DoneValue.StringAddrValue)
12481255
c.Check(value.BoolValue, Equals, t.DoneValue.BoolValue)
12491256
c.Check(value.IntValue, Equals, t.DoneValue.IntValue)
12501257
c.Check(value.Int64Value, Equals, t.DoneValue.Int64Value)

0 commit comments

Comments
 (0)