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. Pass an array of objects to c++ from qml
Forum Update on Monday, May 27th 2025

Pass an array of objects to c++ from qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.7k 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.
  • T Offline
    T Offline
    tham
    wrote on 1 Apr 2018, 10:01 last edited by
    #1

    Trying to pass an array of objects to c++ from qml, but cannot find way to do it properly.

    qml sites

    function getCheckedAudioObject()
    {
        var results = []
        for(var i = 0; i < checkSongIndex.length; ++i){
            var obj = audioModel.get(checkSongIndex[i]) //audioModel is a ListModel
            results.push(obj)            
        }
    
        return results
    }
    

    c++ api

    Q_INVOKABLE void addAudioInformation(QVariantList const &audio_info)
    {
        qDebug()<<__func__<<audio_info.size(); //print 4, as expected
        qDebug()<<__func__<<audio_info[0].typeName(); //print QObject*
        qDebug()<<__func__<<audio_info[0].toMap(); //print Map()
    }
    

    How could I get the data of qml object from c++ api?Thanks

    J 1 Reply Last reply 1 Apr 2018, 10:46
    0
    • T tham
      1 Apr 2018, 10:01

      Trying to pass an array of objects to c++ from qml, but cannot find way to do it properly.

      qml sites

      function getCheckedAudioObject()
      {
          var results = []
          for(var i = 0; i < checkSongIndex.length; ++i){
              var obj = audioModel.get(checkSongIndex[i]) //audioModel is a ListModel
              results.push(obj)            
          }
      
          return results
      }
      

      c++ api

      Q_INVOKABLE void addAudioInformation(QVariantList const &audio_info)
      {
          qDebug()<<__func__<<audio_info.size(); //print 4, as expected
          qDebug()<<__func__<<audio_info[0].typeName(); //print QObject*
          qDebug()<<__func__<<audio_info[0].toMap(); //print Map()
      }
      

      How could I get the data of qml object from c++ api?Thanks

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 1 Apr 2018, 10:46 last edited by
      #2

      @tham said in Pass an array of objects to c++ from qml:

          qDebug()<<__func__<<audio_info[0].typeName(); //print QObject*
      

      The element is a QObject*.

          qDebug()<<__func__<<audio_info[0].toMap(); //print Map()
      

      You cannot convert a QObject* to a QMap.

      First, what does qDebug() << audio_info[0]; print? Is it your own custom QObject? If so, you can add a conversion function to the object.

      Otherwise, you can create a JavaScript object in QML and put this in your array. JavaScript objects can be treated as QVariantMap in C++:

      var results = []
      for(var i = 0; i < checkSongIndex.length; ++i){
          var qObj = audioModel.get(checkSongIndex[i])
          var jObj = {}
          jObj['property1'] = qObj.func1() // Your own function to extract data from the model's element
          jObj['property2'] = qObj.func2()
          results.push(jObj)
      }
      
      
      

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      T 1 Reply Last reply 5 Apr 2018, 14:04
      3
      • J JKSH
        1 Apr 2018, 10:46

        @tham said in Pass an array of objects to c++ from qml:

            qDebug()<<__func__<<audio_info[0].typeName(); //print QObject*
        

        The element is a QObject*.

            qDebug()<<__func__<<audio_info[0].toMap(); //print Map()
        

        You cannot convert a QObject* to a QMap.

        First, what does qDebug() << audio_info[0]; print? Is it your own custom QObject? If so, you can add a conversion function to the object.

        Otherwise, you can create a JavaScript object in QML and put this in your array. JavaScript objects can be treated as QVariantMap in C++:

        var results = []
        for(var i = 0; i < checkSongIndex.length; ++i){
            var qObj = audioModel.get(checkSongIndex[i])
            var jObj = {}
            jObj['property1'] = qObj.func1() // Your own function to extract data from the model's element
            jObj['property2'] = qObj.func2()
            results.push(jObj)
        }
        
        
        
        T Offline
        T Offline
        tham
        wrote on 5 Apr 2018, 14:04 last edited by
        #3

        @JKSH said in Pass an array of objects to c++ from qml:

        Is it your own custom QObject?

        No, it is ListModel of qml.

        J 1 Reply Last reply 5 Apr 2018, 22:44
        0
        • T tham
          5 Apr 2018, 14:04

          @JKSH said in Pass an array of objects to c++ from qml:

          Is it your own custom QObject?

          No, it is ListModel of qml.

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 5 Apr 2018, 22:44 last edited by
          #4

          @tham said in Pass an array of objects to c++ from qml:

          @JKSH said in Pass an array of objects to c++ from qml:

          Is it your own custom QObject?

          No, it is ListModel of qml.

          Then you must convert each audioModel element to a custom JavaScript object. Pass an array of your custom JavaScript objects to C++.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          1

          1/4

          1 Apr 2018, 10:01

          • Login

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