How to pass and modify Javascript object in C++?
Unsolved
QML and Qt Quick
-
So I have the following code:
Item{ id: item property variant dataMap signal fired() onFired:{ qmlHelper.getData(dataMap) console.debug(dataMap[0]; } }
In C++ I have:
void QmlHelper::getProfile(here I want my dataMap) { QUrl url (BASE_URL); QNetworkRequest request(url); QNetworkReply* reply = networkManager->get(); connect(reply,SIGNAL(finished()),this, SLOT(onDataRetrieved())); } void QmlHelper::onDataRetrieved() { QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); if(reply->error() == QNetworkReply::NoError) { // modify my dataMap here } reply->deleteLater(); }
-
@daljit97
Create a QProperty with at least a getter (READ) and changed-signal (NOTIFY)
Then you should be able to use QML's property binding.Alternatively you can connect similiar like you would do in C++:
Component.onCompleted: { qmlHelper.mySignal.connect(onFired) } function onFired(param) { ... }
Both approaches need a signal in your C++ class
-
@daljit97
how did you registerqmlHelper
to the qml engine?
If you did it withqmlRegisterType
then each item can instantiate it's own local member of this type. So you only have a 1:1 relation. -
QmlHelper is a class made available to qml using
QDeclarativeContext::setContextProperty(const QString & name, QObject * value)
Is using qmlRegisterType the only solution? I'd prefer to avoid as that would create other problems to solve in my project.