Skip to content

Commit

Permalink
Go gets the result from calling QML methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Sep 12, 2013
1 parent 56bf76b commit ceab71c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ var tests = []struct {
},
{
Summary: "Call a QML method from Go",
QML: `Item { function f() { console.log("f was called") } }`,
Done: func(d *TestData) { d.compinst.Call("f") },
QML: `Item { function f() { console.log("f was called"); return "<result>"; } }`,
Done: func(d *TestData) { d.Check(d.compinst.Call("f"), Equals, "<result>") },
DoneLog: "f was called",
},
}
Expand Down
5 changes: 3 additions & 2 deletions cpp/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ void objectGetProperty(QObject_ *object, const char *name, DataValue *value)
packDataValue(&var, value);
}

void objectInvoke(QObject_ *object, const char *method)
void objectInvoke(QObject_ *object, const char *method, DataValue *resultdv)
{
QObject *qobject = reinterpret_cast<QObject *>(object);

// TODO Support parameters and result.
// TODO Parameter support.
QVariant result;
QMetaObject::invokeMethod(qobject, method, Qt::DirectConnection, Q_RETURN_ARG(QVariant, result));
packDataValue(&result, resultdv);
}

void objectSetParent(QObject_ *object, QObject_ *parent)
Expand Down
2 changes: 1 addition & 1 deletion cpp/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void contextSetObject(QQmlContext_ *context, QObject_ *value);
void delObject(QObject_ *object);
void objectGetProperty(QObject_ *object, const char *name, DataValue *value);
void objectSetParent(QObject_ *object, QObject_ *parent);
void objectInvoke(QObject_ *object, const char *method);
void objectInvoke(QObject_ *object, const char *method, DataValue *result);

QQmlComponent_ *newComponent(QQmlEngine_ *engine, QObject_ *parent);
void componentSetData(QQmlComponent_ *component, const char *data, int dataLen, const char *url, int urlLen);
Expand Down
3 changes: 3 additions & 0 deletions datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func packDataValue(value interface{}, dvalue *C.DataValue, engine *Engine, owner
}

// unpackDataValue converts a value shipped by C++ into a native Go value.
//
// HEADS UP: This is considered safe to be run out of the main GUI thread.
// If that changes, fix the call sites.
func unpackDataValue(dvalue *C.DataValue) interface{} {
datap := unsafe.Pointer(&dvalue.data)
switch dvalue.dataType {
Expand Down
9 changes: 6 additions & 3 deletions qml.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,17 @@ func (o *commonObject) Field(name string) interface{} {
return unpackDataValue(&value)
}

func (o *commonObject) Call(method string) {
// TODO Support parameters and result.
func (o *commonObject) Call(method string) interface{} {
// TODO Parameter support.
// TODO What about errors?
var result C.DataValue
gui(func() {
// TODO Do not allocate this string every time.
name := C.CString(method)
defer C.free(unsafe.Pointer(name))
C.objectInvoke(o.addr, name)
C.objectInvoke(o.addr, name, &result)
})
return unpackDataValue(&result)
}

// Destroy finalizes the value and releases any resources used.
Expand Down

0 comments on commit ceab71c

Please sign in to comment.