[導讀]Qt Quick中的ListView初學
ListView 3要素:
model : 負責每一個行的 數據
delegate:負責每一個行的 外觀
3.與外界如何交互,刪除和添加數據(通過m
Qt Quick中的ListView初學
3.與外界如何交互,刪除和添加數據(通過model和js)
ListView 3要素:
model : 負責每一個行的 數據 delegate:負責每一個行的 外觀3.與外界如何交互,刪除和添加數據(通過model和js)
通過JavaScript控制邊線,0,2,4,6…..才顯示邊框,用來顯示每一行的界限,界限顏色用Qt自帶的函數就可以了
JavaScript代碼:
Myhelp.js
.pragma library
function 求余數(date) {
var ret =date%2;
console.log("余數:"+ret);
if(ret==0)
{
return 1;
}
return 0;
}
Window {
visible: true
width: 240;
height: 320;
//導出listview的model 供按鈕使用 由于在一個文件中,不需要了
ListView{
id:list_view;
anchors.left: parent.left;
anchors.top: parent.top;
width: parent.width;
height: 180;
model:m_listmodel;
delegate:delegete1;
}
//ListView的model
ListModel{
id:m_listmodel;//model對象中 有數組ListElement 存儲所有的列表數據對象
ListElement{name:"hello"}
}
//listview的代理
Component{
id:delegete1;
Rectangle{
width: parent.width;
height: 40;
color: "#4b3b3b"
border.color: Qt.lighter(color) //顏色暗淡些 不然顯得有點粗
//當序號為2的倍數才有邊框用來分割
border.width: MyHELP.求余數(index);
Text {
anchors.fill: parent;
id: m_txt;
color: "#f3e3e3"
text:name+"序號"+index;
font.family: "Consolas"
font.pointSize: 14
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
}
}
//新建按鈕用來測試
Button{
id:m_btn;
anchors.top: list_view.bottom;
anchors.left:parent.left;
width: parent.width;
height: 40;
text: qsTr("添加");
onClicked: {
m_listmodel.append({name:qsTr(m_input.text)});
}
}
Button{
id:m_delete;
anchors.top: m_btn.bottom;
anchors.left:parent.left;
width: parent.width;
height: 40;
text: qsTr("刪除");
onClicked: {
m_listmodel.remove(m_input.text,1);//刪除指定的項 第二個參數:刪除個數 1就行了不然亂
}
}
TextInput{
id:m_input;
horizontalAlignment: Text.AlignHCenter
font.pointSize: 14
anchors.left:m_delete.left;
anchors.top: m_delete.bottom;
height: 40;
width: parent.width;
color: "#c69191"
autoScroll: true;
text: qsTr("請輸入")
}
}





