• 【QML】ListView 报错 ReferenceError: index is not defined


    问题

    使用ListView的过程中报错:ReferenceError: index is not defined

    错误代码

    import QtQuick 2.15
    import QtQuick.Window 2.15
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Rectangle {
            width: 120
            height: 300
            color: "white"
            ListView {
                id: view
                anchors.fill: parent
                anchors.margins: 20
                clip: true
                model: ListModel{
                    ListElement{name: "admin"}
                    ListElement{name: "jack"}
                    ListElement{name: "tom"}
                }
                delegate: numberDelegate
                spacing: 5
    
                focus: true
            }
    
            Component {
                id: numberDelegate
    
                Rectangle {
                    required property string name //这里出的问题
                    width: ListView.view.width
                    height: 40
                    color: ListView.isCurrentItem?"gray":"lightGray"
                    Text {
                        anchors.centerIn: parent
                        font.pixelSize: 10
                        text: index
                    }
    
                    MouseArea{
                        anchors.fill: parent
                        onClicked:{
                            view.currentIndex = index  //item切换
                            console.log("index"+index)
                        }
                    }
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    解决方法

    • 方法1:
      注释掉required property string name这一行后,问题解决。
    Component {
    	id: numberDelegate
    	
    	 Rectangle {
    	     //required property string name //注释掉
    	     width: ListView.view.width
    	     height: 40
    	     color: ListView.isCurrentItem?"gray":"lightGray"
    	     Text {
    	         anchors.centerIn: parent
    	         font.pixelSize: 10
    	         text: index
    	     }
    	
    	     MouseArea{
    	         anchors.fill: parent
    	         onClicked:{
    	             view.currentIndex = index  //item切换
    	             console.log("index"+index)
    	         }
    	     }
    	 }
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 方法2:
      添加required property int index这一行代码
    Component {
    	id: numberDelegate
    	
    	 Rectangle {
    	     required property string name
    	     required property int index	//添加这一行
    	     width: ListView.view.width
    	     height: 40
    	     color: ListView.isCurrentItem?"gray":"lightGray"
    	     Text {
    	         anchors.centerIn: parent
    	         font.pixelSize: 10
    	         text: index
    	     }
    	
    	     MouseArea{
    	         anchors.fill: parent
    	         onClicked:{
    	             view.currentIndex = index  //item切换
    	             console.log("index"+index)
    	         }
    	     }
    	 }
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    小程序的多种特性
    两表对比脚本CheckData
    Godot引擎小白入门指南
    2023-05-24:为什么要使用Redis做缓存?
    华为云云耀云服务器L实例评测 | 实例使用教学之软件安装:华为云云耀云服务器环境下安装 RabbitMQ 集群
    无感刷新 token
    wget用法随笔
    JAVA中的进制与位运算
    pytest自动化测试-element not interactable、xpath元素定位不到、多页签下子表定位、pip使用问题
    每日三题 9.30
  • 原文地址:https://blog.csdn.net/yangshuoSB/article/details/134057260