Skip to content

Commit 1e309cc

Browse files
committed
Add Context.Spawn.
1 parent faad7c3 commit 1e309cc

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

cpp/capi.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ void contextGetProperty(QQmlContext_ *context, QString_ *name, DataValue *result
327327
packDataValue(&var, result);
328328
}
329329

330+
QQmlContext_ *contextSpawn(QQmlContext_ *context)
331+
{
332+
QQmlContext *qcontext = reinterpret_cast<QQmlContext *>(context);
333+
return new QQmlContext(qcontext);
334+
}
335+
330336
void delObject(QObject_ *object)
331337
{
332338
delete reinterpret_cast<QObject *>(object);

cpp/capi.h

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ void engineAddImageProvider(QQmlEngine_ *engine, QString_ *providerId, void *ima
125125
void contextGetProperty(QQmlContext_ *context, QString_ *name, DataValue *value);
126126
void contextSetProperty(QQmlContext_ *context, QString_ *name, DataValue *value);
127127
void contextSetObject(QQmlContext_ *context, QObject_ *value);
128+
QQmlContext_ *contextSpawn(QQmlContext_ *context);
128129

129130
void delObject(QObject_ *object);
130131
void delObjectLater(QObject_ *object);

qml.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,15 @@ func (ctx *Context) Var(name string) interface{} {
327327
return unpackDataValue(&dvalue, ctx.engine)
328328
}
329329

330-
// TODO Context.Spawn() => Context
330+
// Spawn creates a new context that has ctx as a parent.
331+
func (ctx *Context) Spawn() *Context {
332+
var result Context
333+
result.engine = ctx.engine
334+
RunMain(func() {
335+
result.addr = C.contextSpawn(ctx.addr)
336+
})
337+
return &result
338+
}
331339

332340
// Object is the common interface implemented by all QML types.
333341
//

qml_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,27 @@ func (s *S) TestComponentCreateWindow(c *C) {
282282
window.Hide()
283283
}
284284

285+
func (s *S) TestContextSpawn(c *C) {
286+
context1 := s.engine.Context()
287+
context2 := context1.Spawn()
288+
289+
context1.SetVar("mystr", "context1")
290+
context2.SetVar("mystr", "context2")
291+
292+
data := `
293+
import QtQuick 2.0
294+
Item { property var s: mystr }
295+
`
296+
component, err := s.engine.LoadString("file.qml", data)
297+
c.Assert(err, IsNil)
298+
299+
obj1 := component.Create(context1)
300+
obj2 := component.Create(context2)
301+
302+
c.Assert(obj1.String("s"), Equals, "context1")
303+
c.Assert(obj2.String("s"), Equals, "context2")
304+
}
305+
285306
func (s *S) TestReadVoidAddrProperty(c *C) {
286307
obj := cpptest.NewTestType(s.engine)
287308
addr := obj.Property("voidAddr").(uintptr)

0 commit comments

Comments
 (0)