I admit that QML is not my strong point.
However, since you want to expose a C++ pointer to QML, I don't know if there is any other option but to expose it via a Q_PROPERTY.
So you will have some sort of a C++ QObject class that has a property which is your model pointer and then:
class MyModelExposer
: public QObject{
....
Q_PROPETRY(QSqlQueryModel* sqlQueryModel read sqlQueryModel write setSqlQueryModel NOTIFY onSqlQueryModelChnaged
) ...
pivate:
};
//Somewhere in your C++ code:
qmlRegisterUncreatableType<MyModelExposer>("bar.foo", 1, 0, "MyModelExposer", "");
...
MyModelExposer modelExposerInstance;
...
..
engine.rootContext()->setContextProperty("modelExposer", &modelExposerInstance);
class MyModelExposer : public QObject
{
....
Q_PROPETRY(QSqlQueryModel* sqlQueryModel read sqlQueryModel write setSqlQueryModel NOTIFY onSqlQueryModelChnaged)
...
pivate:
QSqlQueryModel* sqlQueryModel;
};
//Somewhere in your C++ code:
qmlRegisterUncreatableType<MyModelExposer>("bar.foo", 1, 0, "MyModelExposer", "");
...
MyModelExposer modelExposerInstance;
...
..
engine.rootContext()->setContextProperty("modelExposer", &modelExposerInstance);
To copy to clipboard, switch view to plain text mode
And then in QML:
SomeModelConsumer {
model: modelExposer.model
}
SomeModelConsumer {
model: modelExposer.model
}
To copy to clipboard, switch view to plain text mode
Again, may be there are sothers here who are better at QML and can offer a better solution and/or correct my answer if it needs correction.
Bookmarks