删除QML Grid的子项 [英] Delete children of QML Grid

查看:516
本文介绍了删除QML Grid的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 表格{
} 我想循环使用QML Grid的子元素并使用Javascript来销毁每个子元素。 id:contentGrid
spacing:10

ImageItem {imageSource:file:/foo.jpeg} // destroy this
ImageItem {imageSource:file:/bar.jpeg } //销毁这个以及
}

我试图做这样的事情,但($ i = 0; contentGrid.children.length()< i; i ++);

  {
contentGrid.childAt(i).destroy();


解决方案

你有很多问题在你上面的尝试中......首先,你需要向后迭代,因为当你前进的时候你会把孩子的内容向下移动(例如,如果你删除#1,#2会变成孩子#1,然后你会去删除#2这将是旧的孩子#3)。

第二,你需要以不同的方式访问孩子。 childAt()函数用于在屏幕上查找特定的x,y,而不是列表中的某个位置。



试试这个:

 导入QtQuick 1.0 

矩形{
宽度:400
高度:400
Grid {
id:contentGrid
spacing:10

text {text:foo} //销毁这个
Text {text:bar} / /销毁此

MouseArea {
anchors.fill:parent
onClicked:{
for(var i = contentGrid.children.length; i> 0; i--){
console.log(destroying:+ i)
contentGrid.children [i-1] .destroy()
}
}



I want to loop through a QML Grid's children and destroy each of them using Javascript.

Grid {
  id: contentGrid
  spacing: 10

  ImageItem { imageSource: "file:/foo.jpeg"  } // destroy this
  ImageItem { imageSource: "file:/bar.jpeg"  } // destroy this as well
}

I tried to do something like this but it's not working so far.

for(var i = 0; contentGrid.children.length() < i; i++) {
    contentGrid.childAt(i).destroy();
}

解决方案

You have a number of problems in your attempt above... First, you'll need to iterate backwards because you'd be shifting the contents of the children down as you advance (ie, if you delete #1, number #2 would become child #1 and then you'd go to delete #2 which would be the old child #3).

Second, you need to access the children differently. The childAt() function is for locating a child at a particular x,y on the screen, not a position in a list.

Try this instead:

import QtQuick 1.0

Rectangle {
  width: 400
  height: 400
  Grid {
    id: contentGrid
    spacing: 10

    Text { text: "foo"  } // destroy this
    Text { text: "bar"  } // destroy this as well
  }
  MouseArea {
    anchors.fill: parent
    onClicked: {
      for(var i = contentGrid.children.length; i > 0 ; i--) {
        console.log("destroying: " + i)
        contentGrid.children[i-1].destroy()
      }
    }
  }
}

这篇关于删除QML Grid的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆