How to pass and modify Javascript object in C++?

  • So I have the following code:

            id: item
            property variant dataMap
            signal fired()

    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 

  • Moderators

    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: {
        function onFired(param) {

    Both approaches need a signal in your C++ class

  • The problem is I need to create a new dataMap on the go since I use multiple Qml Pages, if I connect a signal from C++ it would be connected to all of my items.

  • Moderators

    how did you register qmlHelper to the qml engine?
    If you did it with qmlRegisterType 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.