找回密码
 立即注册
收起左侧

qml:如何修改多个被点击的Rectangle里边Text的值?

2
回复
8698
查看
[复制链接]
累计签到:4 天
连续签到:1 天
来源: 2018-4-2 18:01:05 显示全部楼层 |阅读模式
1Qter豆
本帖最后由 469514939 于 2018-4-2 18:12 编辑
  1. <div class="blockcode"><blockquote>import QtQuick 2.7
  2. import QtQuick.Controls 2.0
  3. import QtQuick.Layouts 1.3

  4. ApplicationWindow {
  5.     visible: true
  6.     width: 640
  7.     height: 480
  8.     title: qsTr("Hello World")
  9.     id: root

  10.     function changePlace(){
  11.         var list = placeGrid.children.children;

  12.         console.log(list);
  13.         console.log("count: " + list.length);
  14.         for ( var i in list) {
  15.             console.log("list[ " +i + " ] width = " + list[i].width)
  16.             console.log("list[ " +i + " ] height = " + list[i].height)
  17.             console.log("list[ " +i + " ] QQuick = " + list[i])
  18.         }
  19.     }

  20.         GridView {
  21.             clip: true
  22.             id:placeGrid
  23.             width: parent.width
  24.             height: parent.height
  25.             cellWidth: 80
  26.             cellHeight: 80
  27.             model: spaceModel
  28.             delegate: msnDelegate
  29.             focus: true

  30.             Component {
  31.                 id: msnDelegate
  32.                 Item{
  33.                     width: placeGrid.cellWidth
  34.                     height: placeGrid.cellHeight
  35.                     Rectangle {
  36.                         width:parent.width-2
  37.                         height: parent.height-2
  38.                         color: "gray"
  39.                         radius: 5
  40.                         id:place
  41.                         anchors.centerIn: parent
  42.                         Text {
  43.                             id:placeNum
  44.                             text: num
  45.                             color: "white"
  46.                             font.pixelSize: Math.floor(parent.width/2)
  47.                             anchors.centerIn: parent
  48.                         }

  49.                         MouseArea{
  50.                             anchors.fill: parent
  51.                             onClicked: {place.state == "clicked" ? place.state = "" : place.state = "clicked";
  52.                                 console.log(parent.state);
  53.                             }
  54.                         }
  55.                         states: [
  56.                             State{
  57.                                 name: "clicked"
  58.                                 PropertyChanges { target: place; color: "#94a5d4"}
  59.                             }
  60.                         ]



  61.                     }
  62.                 }

  63.             }
  64.             //list模型
  65.             ListModel {
  66.                 id: spaceModel
  67.                 ListElement { code:"A1"; num: 1; stock : 8; rows : "A"; columns:1;}
  68.                 ListElement { code:"A2"; num: 2; stock : 8; rows : "A"; columns:2;}
  69.                 ListElement { code:"A3"; num: 0; stock : 8; rows : "A"; columns:3;}
  70.             }

  71.         }

  72.         Button{
  73.         text: "change"
  74.         onClicked:changePlace();
  75.         y:100
  76.         }

  77. }
复制代码

代码如上,我想修改被选中的矩形里边得数字。先选中矩形,然后点击按钮就统一修改被选中按钮里边数字。

附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册

最佳答案

查看完整内容

建议你保存 点击了的按钮的 index 到 list ,然后 循环 ListModel , indexOf(index) ,如果返回 大于等于0 ,那就改变数字,你把 click 属性修改了,又不做判断,你要干啥?
回复

使用道具 举报

累计签到:742 天
连续签到:1 天
2018-4-2 18:01:06 显示全部楼层
建议你保存 点击了的按钮的 index 到 list ,然后 循环 ListModel , indexOf(index) ,如果返回 大于等于0 ,那就改变数字,你把 click 属性修改了,又不做判断,你要干啥?
参与人数 1人气 +1 收起 理由
469514939 + 1 对我帮助很大!

查看全部评分总评分 : 人气 +1

回复

使用道具 举报

累计签到:4 天
连续签到:1 天
2018-4-2 19:11:48 显示全部楼层
噢噢,谢谢。试了下保存到list然后循环model这方法可以。
我本来得打算是修改states属性后,通过children然后遍历已改变了state的矩形,再改变数字,但是children不到对象
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了