Dynamically create ListModel in QML

前端 未结 3 1222
花落未央
花落未央 2021-02-05 22:37

When I need to create any QML component in runtime, I can use that guide: http://qt-project.org/doc/qt-5/qtqml-javascript-dynamicobjectcreation.html

i.e. just call Qt.cr

3条回答
  •  日久生厌
    2021-02-05 23:05

    I do the initialization like this:

    dataObject_m1.initSystem= function() { // QML calls when ready 
      console.log( "ModulData_1.js func initSystem()");
    
      dataObject_m1.statisticsSystem= Qt.createQmlObject("import QtQuick 2.5; ListModel{}", dataObject_m1.parent, "dynamic_source" );
      dataObject_m1.statisticsSystem.objectName = "ModelSystem";
      dataObject_m1.statisticsSystem.append( { name: qsTr("System"), number: "", val: ""});
      dataObject_m1.statisticsSystem.append( { name: "S3500", number: "", val: ""});
      dataObject_m1.statisticsSystem.append( { name: "S3550", number: "", val: ""});
      dataObject_m1.statisticsSystem.append( { name: "S3551", number: "", val: ""});
      dataObject_m1.statisticsSystem.append( { name: "S9999", number: "", val: ""});
    }    
    

    And to update Data:

     var updateSystem = function ( numberMap , valMap) {
         console.log ("ModuleData_1.js: updateSystem" );
    
       for (var num in (numberMap )) {
          var j = dataObject_m1.idMap[num];
          dataObject_m1.statisticsSystem.set( j, { val : Map[num]});
          console.log ("number(" + numberMap[val]+ ") [" + String(j) + "]= numbeMap["+val+"]" )
      }
     for (var valx in (valMap)) {
         var k = dataObject_m1.idMap[valx];
         dataObject_m1.statisticsSystem.set( k, { bad : valMap[valx]});
         console.log ("val(" + valMap[valx]+ ") [" + String(k) + "]= valMap["+valx+"]" )
      }
    }
    

    Access fuction to model:

     var statisticsModelSystem= function()  {
        console.log ("ModulData_1.js: get statisticsModelSystem" );
        if ( typeof(dataObject_m1.statisticsSystem) !== 'object')
            console.error(" statisticsSystem is undefined ")
    
        return dataObject_m1.statisticsSystem;
    }
    

提交回复
热议问题