Need help passing data to qml
Hi i am having difficulty in passing a struct from cpp to qml. Can someone give me some pointers. I am using qt 5.4/64 bit under debian lenny rc1. Here is what i have been working with which was just a new dialog project with the following added. Note the code seems to work if i pass it just a string but not with a simple struct.
my .h
Code:
namespace Ui {
class Dialog;
struct MyStruct {
int i;
};
}
Q_DECLARE_METATYPE(Ui::MyStruct)
#endif // DIALOG_H
.cpp file
Code:
ui(new Ui::Dialog)
{
ui->setupUi(this);
test();
}
void Dialog::test() {
using namespace Ui;
MyStruct test;
test.i=12;
test.name="Hello world";
QQmlEngine engine;
QQmlComponent component(&engine, "/home/user/projects-qt5/test_qml/MyItem.qml");
QObject *object
= component.
create();
qDebug() << "QML function returned:" << returnedValue.toString();
qDebug() << "comp error is " << component.errors();
delete object;
}
.qml
Code:
import QtQuick 2.0
// MyItem.qml
Item {
function myQmlFunction(v) {
console.log("Got message:",v.name)
return "some return value"
}
}
Re: Need help passing data to qml
It doesn't work like that. Wrapping a structure into a variant will not teach Qt to understand its structure and thus be able to access its members. For that you should derive your class from QObject and make members into properties or you should use QVariantMap and manually copy each field value to the map.
Re: Need help passing data to qml
Quote:
Originally Posted by
brasscs
Note the code seems to work if i pass it just a string but not with a simple struct.
You can pass any type that is wrapable in a QVariant.
If you mean that you can't access the data inside the variant, then see wysota's comment.
Cheers,
_
Re: Need help passing data to qml
ok so how about an example of passing a struct. gave up finding something online
Re: Need help passing data to qml
Quote:
Originally Posted by
brasscs
ok so how about an example of passing a struct. gave up finding something online
Code:
MyStruct s;
QVariantMap map;
map["i"] = s.i;
map["name"] = s.name;
Re: Need help passing data to qml
hey thanks for the code but it still does not work. this is what i am using
.cpp
Code:
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;
.h
Code:
namespace Ui {
class Dialog;
struct MyStruct {
int i;
};
}
...
Q_DECLARE_METATYPE(Ui::MyStruct)
#endif // DIALOG_H
.qml
Code:
import QtQuick 2.0
// MyItem.qml
Item {
function myQmlFunction(v) {
console.log("Got message:",v.name)
return "some return value"
}
}
app output
qml: QML Got message: undefined
QML function returned: "some return value"
comp error is ()
Re: Need help passing data to qml
It works just fine for me.
Code:
#include <QtGui>
#include <QtQml>
int main(int argc, char **argv) {
QGuiApplication app(argc, argv);
QQmlEngine e;
QQmlComponent c(&e, "test.qml");
QVariantMap m;
m["i"] = 10;
m["name"] = "someName";
// return app.exec();
return 0;
}
Code:
import QtQml 2.0
QtObject {
function testFunc(v) { console.log("v.name = ", v.name); }
}
Re: Need help passing data to qml
hey wysota
i finally found my error. the code you supplied is now working fine.
thanks for your help.