QML 动态创建对象
创建:
var xpos = (left_X- width*0.5) var ypos = getY(minPrice) var bshow = false var src2 = 'import QtQuick 2.0;import "qrc:/controls";RT_XdrInfo {x:%1;y:%2;show:false ;dat:%3;}'; src2 = src2.arg((root.canvas._canvas.width -xpos )<120? (root.canvas._canvas.width -130):xpos+ 30).arg(ypos + 30).arg(JSON.stringify(xdr)) var newObject2 = Qt.createQmlObject(src2,root.canvas._canvas, "showxdrInfo"); if (newObject2 === null) { // Error Handling console.log("Error creating object new xdr info Object!"); }else{ var xdrinfodata ={ time:time, obj:newObject2, } xdrInfoarr.push(xdrinfodata) } var src = 'import QtQuick 2.0 ;import "qrc:/controls";Text {id: "flgicon";x: %1;y:%2;font.family: "iconfont";text: String.fromCharCode(0xe65b);color: "#FF0000";font.pixelSize: 24;MouseArea {id: flgArea;width: parent.width;height: parent.height;anchors.fill: parent ;hoverEnabled: true;onEntered:{onShowInfo(%3,true);} onExited: {onShowInfo(%3,false);}} } ' src = src.arg(xpos).arg(ypos).arg(time.toString()) var newObject = Qt.createQmlObject(src,root.canvas._canvas, "mouseItem"); if (newObject === null) { // Error Handling console.log("Error creating object flg icon!"); }else{ flgxdrarr.push(newObject) } }
清理:
if(flgxdrarr.length) { for(var i=0;i<flgxdrarr.length;i++) flgxdrarr[i].destroy(); } flgxdrarr = [] if(xdrInfoarr.length) { for(var i=0;i<xdrInfoarr.length;i++) xdrInfoarr[i].obj.destroy(); } xdrInfoarr = []
打赏: