QML之ListView列表视图

20 篇文章 3 订阅

Rectangle{
        x:20
        y:20
        height: 50
        width: 50
        radius: 5
        color: mous.pressed ? "red" :"gray"
        Text {
            anchors.centerIn: parent
            font.pixelSize: 30
            text: qsTr("+")
        }
        MouseArea{//一些操作获取方式
            id:mous
            anchors.fill: parent
            onClicked: {mylist.incrementCurrentIndex();
                //mylist.model.append({"name":"黄昏","score":88})//追加了一项数据
                //mylist.model.insert(2,{"name":"落日","score":88})//插入一项数据,index为2
                //mylist.model.move(0,5,2);//移动,第一个参数表示从index为0开始,移动个数为第三个参数
                                         //第二个参数为移动后的index
                //mylist.model.set(2,{"name":"落日","score":88})//重新设置改变index为2的数据值
                //注意,如果set的index值等于count,则添加一个新的数据,相当append。
                //mylist.model.setProperty(2,"name","夕阳")//设置index数据,一次只能设置一个。index得小count
                //mylist.model.sync()// can only be called from a WorkerScript
 
            }
        }
    }
 
    Rectangle{
        x:960
        y:20
        height: 50
        width: 50
        radius: 5
        color: mous1.pressed ? "red" :"gray"
        Text {
            anchors.centerIn: parent
            font.pixelSize: 30
            text: qsTr("-")
        }
        MouseArea{//一些操作获取方式
            id:mous1
            anchors.fill: parent
            onClicked: {//mylist.decrementCurrentIndex();
                //mylist.model.clear();//全部清除数据
                //console.log(mylist.model.get(1).score);//get获取到第几个Index的信息
                //mylist.model.remove(1);//删除掉参数对应的index数据
 
            }
        }
    }
 
   Component{
       id:myfooter
       Rectangle{
        height: 50
        width: 400
        color: "red"
        Text {
            anchors.centerIn: parent
            font.pixelSize: 30
            text: mylist.count
        }
 
       }
   }
   Component{
       id:myheader
       Rectangle{
        height: 50
        width: 400
        color: "red"
        Text {
            anchors.centerIn: parent
            font.pixelSize: 15
            text: "count:" + mylist.count + "  currentSection:" + mylist.currentSection
        }
       }
   }
 
   Component{
       id:myhighlight
       Rectangle{
        z:2
        height: 50
        width: 400
        color: "green"
 
       }
   }
 
    ListView {
        id:mylist
        height: 400
        width: 400
        y:50
        anchors.horizontalCenter: parent.horizontalCenter
        //count: 5 //只读属性
        //currentSection: 只读属性
        //currentIndex: 2//当前项
        spacing: 10 //子项之间的间隙
        footer:myfooter //列表的页脚,类型Component
        //header: myheader //列表的标题或者说是头部
        //highlight: myhighlight  //高亮显示
        //highlightFollowsCurrentItem: true//当前项改变的时候,高亮显示会跟着移动
        //highlightMoveDuration: 500
        //highlightMoveSpeed: 200 //高亮的移动速度
        //highlightRangeMode: ListView.ApplyRange
        //ListView.StrictlyEnforceRange
        //ListView.NoHighlightRange
        //highlightResizeDuration: 100
         //highlightResizeSpeed: 500
        //highlightItem只读属性
       // preferredHighlightBegin: 2
        //preferredHighlightEnd: 5
 
        keyNavigationWraps: true  //当前项头尾互切(到达最后一项再增加到下一项即可返回第一项)incrementCurrentIndex()
        //orientation: ListView.Horizontal//列表的方向,水平
        orientation:ListView.Vertical //默认垂直方向
        layoutDirection: Qt.LeftToRight //水平布局方向
        //layoutDirection: Qt.RightToLeft
        //section.criteria: ViewSection.FullString //
        //section.criteria: ViewSection.FirstCharacter
        //section.delegate: myheader
        //section.property: "hello"
 
        //snapMode: ListView.NoSnap //拖动停止设置默认,可见区域任意点可停止
        //snapMode: ListView.SnapToItem //
        //snapMode: ListView.SnapOneItem//移动一个Item,对于一次翻转一个界面很有用
 
        delegate: mycomponent //委托,进行实例显示
        model:mymodel //myvisual//数据模型
        //数据模型有, ListModel ,XmlListModel,VisualItemModel,还有C++提供的数据模型
        
        
        Component{//Component把元素封装成一个组件,作为列表的委托,实例显示
                  //一个.qml文件也是一个组件,一样可以最委托
            id:mycomponent
            Rectangle{
                height: 60
                width: 400
                //color: mylist.isCurrentItem ? "red" : "gray"//是否为当前项,我的没起作用
                color: mylist.currentIndex ==index ? "red" : "gray"//当前光标是否等于index,个人感觉和isCurrentItem这个属性是一样的作用
                Text{
                    anchors.centerIn: parent
                    font.pixelSize: 25
                    text:name +": " +score //从数据模型里面获取的数据
                }
                
            }
        }
 
        ListModel{//简单的ListElement容器
            id:mymodel
            ListElement{name:"张三";score:82} //可以添加很多数据内容,委托给delegate显示
            ListElement{name:"李四";score:75}
            ListElement{name:"王五";score:96}
            ListElement{name:"黄六";score:76}
            ListElement{name:"启华";score:25}
            ListElement{name:"京东";score:68}
            ListElement{name:"淘宝";score:35}
            ListElement{name:"乐视";score:46}
            ListElement{name:"三星";score:78}
            ListElement{name:"华为";score:59}
            ListElement{name:"电信";score:82}
            ListElement{name:"移动";score:52}
            ListElement{name:"小米";score:99}
            ListElement{name:"联通";score:27}
            ListElement{name:"魅族";score:89}
        }
 
        VisualItemModel{//使用VisualItemModel数据模型不需要委托显示
                        //数据模型中是是含有元素的,根据index来显示不同的item
            id:myvisual
            Rectangle{height: 100;width: 400;radius: 5;color: "red"}
            Rectangle{height: 100;width: 400;radius: 5;color: "blue"}
            Rectangle{height: 100;width: 400;radius: 5;color: "yellow"}
            Rectangle{height: 100;width: 400;radius: 5;color: "#567890"}
            Rectangle{height: 100;width: 400;radius: 5;color: "#887766"}
 
 
        }
    }
 说明:简单点来说,列表视图只要是分为三个部分,一个是列表控制元素listview,一个是数据模型model,一个是delegate委托代理实例化显示model的数据。
需要注意的是,当model为Visual了ItemModel时候不需要delegate代理显示,因为这个model本身就是用来显示的。
 

 
 
 
 
 
 
 
 

                
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Qt QML 提供了一个名为 ListView 的组件,可以用于显示列表视图,支持下拉刷新、上拉分页和滚动轴。 以下是一个简单的 ListView 示例: ``` import QtQuick 2.0 import QtQuick.Controls 2.0 ApplicationWindow { visible: true width: 400 height: 600 title: "ListView Example" ListView { id: listView anchors.fill: parent // 设置模型数据 model: ListModel { ListElement { name: "Item 1" } ListElement { name: "Item 2" } ListElement { name: "Item 3" } ListElement { name: "Item 4" } ListElement { name: "Item 5" } ListElement { name: "Item 6" } ListElement { name: "Item 7" } ListElement { name: "Item 8" } ListElement { name: "Item 9" } ListElement { name: "Item 10" } } // 设置每个项的属性 delegate: Rectangle { width: listView.width height: 50 color: index % 2 == 0 ? "#f2f2f2" : "#ffffff" Text { text: name anchors.centerIn: parent } } // 下拉刷新 onRefreshRequested: { console.log("下拉刷新") } // 上拉分页 onEndReached: { console.log("上拉分页") } // 滚动轴 ScrollBar.vertical: ScrollBar {} } } ``` 上面的示例中,ListViewmodel 属性设置了一个 ListModel,用于存储模型数据。delegate 属性则定义了每个项的属性,包括宽度、高度、颜色和文本。 在 ListView 中可以通过 onRefreshRequested 和 onEndReached 信号来实现下拉刷新和上拉分页功能。当用户下拉列表视图时,会触发 onRefreshRequested 信号;当滚动到列表视图底部时,会触发 onEndReached 信号。 最后,为了实现滚动轴,我们可以在 ListView 中添加 ScrollBar 组件,设置其 vertical 属性为 true 即可。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值