电竞比分网-中国电竞赛事及体育赛事平台

分享

Qml中ListView用法

 3D建模仿真 2015-10-10

一、ListView列表視圖。

如果你了解Qt的模型視圖結(jié)構(gòu),那么這一節(jié)的內(nèi)容就很好理解了,如果你沒接觸過,也沒關(guān)系,因?yàn)樗鋵?shí)很簡(jiǎn)單。在Qt中我們要想利用視圖顯示一些數(shù)據(jù),并不是將這些數(shù)據(jù)直接放到視圖中的,因?yàn)橐晥D只管顯示,它不存儲(chǔ)數(shù)據(jù)。我們的數(shù)據(jù)要放在數(shù)據(jù)模型中。但是數(shù)據(jù)模型中只是存放數(shù)據(jù),它并不涉及數(shù)據(jù)的顯示方式。所以,我們還要用一個(gè)叫做代理的東東來設(shè)置數(shù)據(jù)模型中的數(shù)據(jù)怎樣在視圖中顯示。那么就構(gòu)成了下面的關(guān)系。

 QML 視圖

我們先看下面的例子:

1、新建一個(gè)Qt QML Application工程,命名為“myView”。

2、我們更改代碼如下:

  1. import Qt 4.6  
  2. Rectangle {  
  3. width:200;height:200  
  4. ListModel{  //數(shù)據(jù)模型  
  5. id:listModel  
  6. ListElement{name:”Tom”;number:”001″}  
  7. ListElement{name:”John”;number:”002″}  
  8. ListElement{name:”Sum”;number:”003″}  
  9. }  
  10. Component{     //代理  
  11. id:delegate  
  12. Item{ id:wrapper; width:200; height:40  
  13. Column{  
  14. x:5; y:5  
  15. Text{text:”<b>Name:</b>”+name}  
  16. Text{text:”<b>Number:</b>”+number}  
  17. }  
  18. }  
  19. }  
  20. Component{   //高亮條  
  21. id:highlight  
  22. Rectangle{color:”lightsteelblue”;radius:5}  
  23. }  
  24. ListView{  //視圖  
  25. width:parent.width; height:parent.height  
  26. model:listModel  //關(guān)聯(lián)數(shù)據(jù)模型  
  27. delegate:delegate  //關(guān)聯(lián)代理  
  28. highlight:highlight  //關(guān)聯(lián)高亮條  
  29. focus:true  //可以獲得焦點(diǎn),這樣就可以響應(yīng)鍵盤了  
  30. }  

運(yùn)行效果如下:

 QML 視圖

我們可以拖動(dòng)整個(gè)列表,而且可以使用鍵盤的方向鍵來選擇列表中的項(xiàng)目。

在這個(gè)程序中,我們先設(shè)置了數(shù)據(jù)模型,在其中加入了一些數(shù)據(jù)。然后設(shè)置了代理,在代理中我們?cè)O(shè)置了要怎樣顯示我們的數(shù)據(jù)。最后,我們?cè)谝晥D中關(guān)聯(lián)了數(shù)據(jù)模型和代理,將數(shù)據(jù)顯示出來了。這里為了達(dá)到更好的顯示效果,我們使用了一個(gè)高亮條。其中的代理和高亮條都可以使用Component{}組件來實(shí)現(xiàn)。

3.我們可以對(duì)視圖做一些設(shè)置。

我們可以設(shè)置keyNavigationWraps:true 使到達(dá)最后一個(gè)項(xiàng)目后重新返回第一個(gè)項(xiàng)目。

我們可以設(shè)置orientation:ListView.Horizontal使列表水平顯示。這時(shí)你拖動(dòng)列表,發(fā)現(xiàn)了吧,它可以自動(dòng)移動(dòng)到下一條,這就是Flickable的作用。默認(rèn)的是ListView.Vertical豎直顯示。

ListView的delegate有兩種方式。
第一種方式:直接定義delegate。
ListView {
        id: listView1        

        delegate: Item {            

            Rectangle {
                id:cellRect
                anchors.fill: parent
                border.width: 1
                border.color: "#5f5858"

                Text {
                    id:itemText
                    text: name
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
        }
        model: ListModel {
            id:testModel
            ListElement {
                name: "成都"
            }
        }
    }

其它地方使用
 testModel.clear();
testModel.append({"name":"北京"});
testModel.append({"name":"上海"});


第二種方式:
將delegate定義成組件,在ListView中加用其id。
ListView {
        id: listView1        

        delegate: appDelegate//引用組件
        model: ListModel {
            id:testModel
            ListElement {
                name: "成都"
            }
        }
    }
//組件定義,在原有Item基礎(chǔ)上加在Component {id: appDelegate...}
Component {
        id: appDelegate
        Item {            

            Rectangle {
                id:cellRect
                anchors.fill: parent
                border.width: 1
                border.color: "#5f5858"

                Text {
                    id:itemText
                    text: name
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
        }
    }
其它地方使用
 testModel.clear();
testModel.append({"name":"北京"});
testModel.append({"name":"上海"});

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多