Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to pass and modify Javascript object in C++?

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmljavascriptc++
5 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    daljit97
    wrote on last edited by
    #1

    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();
    }
    
    raven-worxR 1 Reply Last reply
    0
    • D daljit97

      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();
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daljit97
        wrote on last edited by
        #3

        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.

        raven-worxR 1 Reply Last reply
        0
        • D daljit97

          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.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @daljit97
          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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • D Offline
            D Offline
            daljit97
            wrote on last edited by daljit97
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved