hey thanks for the code but it still does not work. this is what i am using
.cpp
using namespace Ui;
QQmlEngine engine;
QQmlComponent component(&engine, "/home/user/projects-qt5/test_qml/MyItem.qml");
QObject *object
= component.
create();
MyStruct s;
s.i=12;
s.name="Hello QML";
QVariantMap map;
map["i"] = s.i;
map["name"] = s.name;
using namespace Ui;
QQmlEngine engine;
QQmlComponent component(&engine, "/home/user/projects-qt5/test_qml/MyItem.qml");
QObject *object = component.create();
QVariant returnedValue;
MyStruct s;
s.i=12;
s.name="Hello QML";
QVariantMap map;
map["i"] = s.i;
map["name"] = s.name;
QVariant v = map;
QMetaObject::invokeMethod(object, "myQmlFunction",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant,v));
To copy to clipboard, switch view to plain text mode
.h
namespace Ui {
class Dialog;
struct MyStruct {
int i;
};
}
...
Q_DECLARE_METATYPE(Ui::MyStruct)
#endif // DIALOG_H
namespace Ui {
class Dialog;
struct MyStruct {
int i;
QString name;
};
}
...
Q_DECLARE_METATYPE(Ui::MyStruct)
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode
.qml
import QtQuick 2.0
// MyItem.qml
Item {
function myQmlFunction(v) {
console.log("Got message:",v.name)
return "some return value"
}
}
import QtQuick 2.0
// MyItem.qml
Item {
function myQmlFunction(v) {
console.log("Got message:",v.name)
return "some return value"
}
}
To copy to clipboard, switch view to plain text mode
app output
qml: QML Got message: undefined
QML function returned: "some return value"
comp error is ()
Bookmarks