当前位置: 首页 > article >正文

【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:
    注释掉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)
	         }
	     }
	 }
	}
  • 方法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)
	         }
	     }
	 }
	}

http://www.kler.cn/a/104784.html

相关文章:

  • CSP-J2023入门组第二轮T4:旅游巴士
  • jenkins、ant、selenium、testng搭建自动化测试框架
  • TWAS模型数据*.wgt.RDat查看及导入R
  • 常见的主流自动化测试框架,这5种能帮到你很多
  • 华为OD机考算法题:支持优先级的队列
  • 蓝桥杯双周赛算法心得——铺地板(质因数)
  • 支付宝支付接入流程
  • 【强化学习】09——价值和策略近似逼近方法
  • html计算器
  • TypeScript中的元组类型
  • 百度地图设置和关闭label弹窗
  • ES6中的新增属性——解构赋值
  • Java-API简析_java.net.URL类(基于 Latest JDK)(浅析源码)
  • 美国访问学者申请签证的五点建议
  • 每日一练——快速合并2个有序数组
  • 分布式ID系统设计(1)
  • 个人用户免费,亚马逊正式推出 AI 编程服务 CodeWhisperer
  • C语言之结构体和共用体详解
  • 初探亚马逊 AI 编程助手 CodeWhisperer
  • 数据挖掘和大数据的区别