QML-如何让孩子加入物品? [英] QML - How to get children in an item?

查看:94
本文介绍了QML-如何让孩子加入物品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对QML中的商品有疑问.我想让一个项目的孩子,但它似乎在第一个元素中起作用.

I had a problem with the item in QML. I wanna get children of an item but it seems working in the first element.

详细代码如下:

我有一个带有列表自定义组件AAA_Styles的gridview

I have a gridview with a list custom component AAA_Styles

GridView{
    id: grdViewDeviceControl
    clip: true
    interactive: true
    ScrollIndicator.vertical: ScrollIndicator{}
    cellWidth: 200
    cellHeight: 300

    model: ListModel{}

    delegate: Item {
        width: grdViewDeviceControl.cellWidth
        height: grdViewDeviceControl.cellHeight
        AAA_Styles{
            id: deviceControl
            objectName: "deviceControl"
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            name: Names
            subname: Subnames
    }
}

我的自定义 AAA_RTS QML 组件,具有一些功能,例如:
-function_a()
-function_b()
我使用

My custom AAA_RTS is a QML component have some functions such as:
- function_a()
- function_b()
I added 2 items into model using

grdViewDeviceControl.model.append()

grdViewDeviceControl.model.append()

我确保模型具有添加的数据,因为它出现在设计中并且gridview的数量为2个元素

I ensure the model have data that I added because It appeared in my design and the count of gridview is 2 elements

console.log(grdViewDeviceControl.count)//结果为2

console.log(grdViewDeviceControl.count) //the result is 2

此后,我尝试使用按钮信号 onRelease 中的方法来获取每个元素以访问它们可用的功能:

After that, I tried to get each element to access functions that they are available using a method in signal onRelease of a button:

onReleased: {
    console.log("number of item: " + grdViewDeviceControl.count)
    var myRTS = grdViewDeviceControl.contentItem.children[0]
    console.log(myRTS)
    console.log(myRTS.children[0])
    myRTS = grdViewDeviceControl.contentItem.children[1]
    console.log(myRTS)
    console.log(myRTS.children[1])
}

控制台上的结果:

qml:件数:2
qml:QQuickItem(0x9828f60)
qml:AAA_Styles_QMLTYPE_0_QML_112(0x9829070,"deviceControl")
qml:QQuickItem(0x5554140)
qml:未定义

qml: number of item: 2
qml: QQuickItem(0x9828f60)
qml: AAA_Styles_QMLTYPE_0_QML_112(0x9829070, "deviceControl")
qml: QQuickItem(0x5554140)
qml: undefined

有了第一个元素 grdViewDeviceControl.contentItem.children [0] ,我成功访问了 function_a function_b 但是当我使用第二个错误时出现了

With the first element grdViewDeviceControl.contentItem.children[0], I access function_a or function_b successful but when I using the second the error appeared

TypeError:无法调用未定义的方法'function_a'

那么谁能告诉我为什么我做错了以及如何解决它?

So can anyone tell me why I wrong and how to fix it?

非常感谢您的帮助!

推荐答案

请勿尝试直接访问子项.改为使用代表ID,信号和插槽:

Do not try to access directly to the child items. Use delegate IDs, signals and slots instead:

  • 通过您的GridView模型为所有代表提供代表ID".
  • 在您的GridView中,添加将用于向所有代表广播的信号,包括以下内容:
    • 您希望其执行功能的委托人的委托人ID".
    • AAA_Styles函数的参数.
    • Give a "delegate ID" to all your delegates through the model of your GridView.
    • In your GridView, add signals that will be used to broadcast to all the delegates the following things:
      • The "delegate ID" of the delegate that you want it to execute the function.
      • The arguments for the AAA_Styles function.

      以下代码总结了我刚刚向您描述的内容.如果不起作用,请将委托放入单独的QML文件中.这应该很好:

      The following piece of code sums up what I have just described to you. If it does not work put the delegate in a separated QML file. This should work for good:

      // Your grid
      GridView {
          id: grdViewDeviceControl
          clip: true
          interactive: true
          ScrollIndicator.vertical: ScrollIndicator {}
          cellWidth: 200
          cellHeight: 300
      
          model: ListModel {
              /*
                  Example of list element:
                  ListElement { m_uuid: "{element-uuid}" }
              */
          }
      
          delegate: Item {
              width: grdViewDeviceControl.cellWidth
              height: grdViewDeviceControl.cellHeight
      
              AAA_Styles {
                  id: deviceControl
                  objectName: "deviceControl"
                  anchors.centerIn: parent
                  name: Names
                  subname: Subnames
              }
      
              // The delegate ID
              property string delegate_id: m_uuid
      
              // Broadcast receivers
              function delfunc_a(uuid, argA0) {
                  if (uuid === this.delegate_id) {
                      deviceControl.function_a(argA0)
                  }
              }
      
              function delfunc_b(uuid, argB0, argB1) {
                  if (uuid === this.delegate_id) {
                      deviceControl.function_b(argB0, argB1)
                  }
              }
      
              // Connecting broadcasters to receivers
              Component.onCompleted: {
                  grdViewDeviceControl.exec_a.connect(this.delfunc_a)
                  grdViewDeviceControl.exec_b.connect(this.delfunc_b)
              }
      
              Component.onDestruction: {
                  grdViewDeviceControl.exec_a.disconnect(this.delfunc_a)
                  grdViewDeviceControl.exec_b.disconnect(this.delfunc_b)
              }
          }
      
          // Your broadcasters
          signal exec_a(string uuid, int argA0)
          signal exec_b(string uuid, bool argB0, string argB1)
      }
      

      // Somewhere else in your code:
      onReleased: {
          /*
           * Only the delegate whose ID is "{a-given-uuid}"
           * will execute deviceControl.function_a(3):
           */
          grdViewDeviceControl.exec_a("{a-given-uuid}", 3)
          /*
           * Only the delegate whose ID is "{another-given-uuid}"
           * will execute deviceControl.function_b(true, "U got style!"):
           */
          grdViewDeviceControl.exec_b("{another-given-uuid}", true, "U got style!")
      }
      

      这篇关于QML-如何让孩子加入物品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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