diff --git a/all_test.go b/all_test.go index 824b2077..07e6fa46 100644 --- a/all_test.go +++ b/all_test.go @@ -558,6 +558,14 @@ var tests = []struct { Done: func(d *TestData) { d.compinst.Call("log", d.compinst) }, DoneLog: "Width is 300", }, + { + Summary: "Create a QML-defined component in Go", + QML: `Item { property var comp: Component { Rectangle { width: 300 } } }`, + Done: func(d *TestData) { + rect := d.compinst.Field("comp").(*qml.Value).Create(nil) + d.Assert(rect.Field("width"), Equals, float64(300)) + }, + }, } var tablef = flag.String("tablef", "", "if provided, TestTable only runs tests with a summary matching the regexp") diff --git a/cpp/capi.cpp b/cpp/capi.cpp index 5f601da3..6f5b54ae 100644 --- a/cpp/capi.cpp +++ b/cpp/capi.cpp @@ -107,6 +107,9 @@ QObject_ *componentCreate(QQmlComponent_ *component, QQmlContext_ *context) QQmlComponent *qcomponent = reinterpret_cast(component); QQmlContext *qcontext = reinterpret_cast(context); + if (!qcontext) { + qcontext = qmlContext(qcomponent); + } return qcomponent->create(qcontext); } diff --git a/qml.go b/qml.go index 5b46009d..fac0502e 100644 --- a/qml.go +++ b/qml.go @@ -334,6 +334,20 @@ func (o *commonObject) Call(method string, params ...interface{}) interface{} { return unpackDataValue(&result, o.engine) } +func (o *commonObject) Create(context *Context) *Value { + // TODO Fail if o does not represent a component. + var value Value + value.engine = o.engine + gui(func() { + ctxaddr := nilPtr + if context != nil { + ctxaddr = context.addr + } + value.addr = C.componentCreate(o.addr, ctxaddr) + }) + return &value +} + // Destroy finalizes the value and releases any resources used. // The value must not be used after calling this method. func (o *commonObject) Destroy() {