北京天仪建设工程质量检测所网站,辽宁城市建设网站,上海网页制作哪家好,1 高端品牌网站定制LIstModel#xff08;列表模型#xff09;
ListModel 是ListElement定义的简单容器#xff0c;每个定义都包含数据角色。内容可以在 QML 中动态定义或显式定义。
属性#xff1a;
count模型中数据条目的数量dynamic动态角色#xff0c;默认情况下#xff0c;角色的类型…LIstModel列表模型
ListModel 是ListElement定义的简单容器每个定义都包含数据角色。内容可以在 QML 中动态定义或显式定义。
属性
count模型中数据条目的数量dynamic动态角色默认情况下角色的类型在首次使用角色时是固定的
方法
append添加新项到模型末尾clear从模型中删除所有内容getint index返回列表模型中索引处的项。这允许从 JavaScript 访问或修改项目数据insert将新项添加到位于位置索引的列表模型中并将值置于字典中move将 n 个项目从一个位置移动到另一个位置remove从模型中删除索引处的内容set更改列表模型中索引处的项setProperty修改指定位置的属性sync从工作线程脚本修改列表模型后将任何未保存的更改写入列表模型。
在列表容器中添加列表项 ListElement添加的规则 必须以小写字母开头并且对于给定模型中的所有元素都应通用值必须是简单的常量;字符串布尔值真、假、数字或枚举值ListModel{id:model1ListElement{ //使用ListElement添加列表数据项name:1111age:12}ListElement{name:2222age:13}}
使用ListView显示 使用ListView获取ListModel通过delegate委托来选择格式来显示数据 ListModel{id:model1ListElement{name:1111age:12}ListElement{name:2222age:13}}ListView{anchors.fill: parentmodel: model1delegate: Row{Text{text:名字name}Text{text:年龄age}}} 使用Component组键包装用来显示信息
Window {id:window1visible: truewidth: 700height: 700title: qsTr(Hello World)ListModel{id:model1ListElement{name:1111age:12}ListElement{name:2222age:13}}Component{id:component1Row{spacing: 10Text{text:名字name}Text{text:年龄age}}}ListView{anchors.fill: parentmodel: model1delegate: component1 //设置委托}
}在ListElement中嵌套ListElement的处理 ListModel {id: fruitModelListElement {name: 小明age: 13attributes: [ListElement { description: 学生 },ListElement { description: 学习委员 }]}ListElement {name: 王老师age: 30attributes: [ListElement { description: 班主任 }]}ListElement {name: 张三age: 13attributes: [ListElement { description: 学生 },ListElement { description: 体育委员 }]}}Component {id: fruitDelegateItem {width: 200; height: 50Text { id: nameField; text: name }Text { text: : age; anchors.left: nameField.right }Row {anchors.top: nameField.bottomspacing: 5Text { text: 身份 }Repeater {model: attributesText { text: description }}}}}ListView{anchors.fill: parentmodel:fruitModeldelegate: fruitDelegate} 函数的使用 ListModel{id:model1ListElement{name:1111age:12}ListElement{name:2222age:13}}//输出列表项的个数
Component.onCompleted: {console.log(列表项的个数,Model.count)}
//添加数据到尾部
Component.onCompleted: {model1.append({name:3333,age:13})}
//插入数据
Component.onCompleted: {model1.insert(1,{name:4444,age:13})}
//获取数据Component.onCompleted: {var datamodel1.get(1).name;model1.get(1).name5555model1.get(1).age15}
//移动数据
Component.oncompleted:{model1.move(0,model1.count-3,3);//前 3 项移动到列表末尾}
//删除数据
Component.oncompleted:{model1.remove(2);//删除索引处的内容}
//清空
Component.oncompleted:{model1.clear();//清空}
//修改索引处的值
Component.oncompleted:{model1.set(1,{name:3222,age:13})}
//设置索引处的属性
Component.oncompleted:{model1.setProperty(1,name,12222)} 列表模型和WorkerScript的使用 listModel可以和WorkerScript一起使用可以从多线程访问列表可以将列表操作移动到其他线程以避免阻塞主 GUI 线程。 定时添加数据 Timer { //定时器id: timerinterval: 2000; repeat: truerunning: truetriggeredOnStart: trueonTriggered: { //定时器触发var msg {action: appendCurrentTime, model: listModel};worker.sendMessage(msg);}}WorkerScript.onMessage function(msg) {if (msg.action appendCurrentTime) {var data {time: new Date().toTimeString()};msg.model.append(data); //添加数据msg.model.sync(); // 更新列表}
}
文章转载自: http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.dzfwb.cn.gov.cn.dzfwb.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.hpprx.cn.gov.cn.hpprx.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.djwpd.cn.gov.cn.djwpd.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.prmyx.cn.gov.cn.prmyx.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.qlpq.cn.gov.cn.qlpq.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.wmdqc.com.gov.cn.wmdqc.com http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.kqpxb.cn.gov.cn.kqpxb.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.tpnxj.cn.gov.cn.tpnxj.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.qdxkn.cn.gov.cn.qdxkn.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.trsmb.cn.gov.cn.trsmb.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.lmmh.cn.gov.cn.lmmh.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.yktr.cn.gov.cn.yktr.cn http://www.morning.fxzlg.cn.gov.cn.fxzlg.cn http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.jpjxb.cn.gov.cn.jpjxb.cn http://www.morning.qnklx.cn.gov.cn.qnklx.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.zkzjm.cn.gov.cn.zkzjm.cn http://www.morning.mbzlg.cn.gov.cn.mbzlg.cn http://www.morning.mwjwy.cn.gov.cn.mwjwy.cn http://www.morning.c7513.cn.gov.cn.c7513.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn