Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 如何将动态内容放置到QML组件中_Qt_Qml_Qt5 - Fatal编程技术网

Qt 如何将动态内容放置到QML组件中

Qt 如何将动态内容放置到QML组件中,qt,qml,qt5,Qt,Qml,Qt5,我在QML中创建可扩展组件时遇到了很多问题。有一些技术可行,但我想做的是这样的: //Outline.qml Rectangle { color: 'red' //Children are inserted here } //main.qml Rectangle { ... Outline { Text { I'm trying to inject this into the Outline component text: "Hi I'm the inne

我在QML中创建可扩展组件时遇到了很多问题。有一些技术可行,但我想做的是这样的:

//Outline.qml
Rectangle {
  color: 'red'
  //Children are inserted here
}

//main.qml
Rectangle {
  ... 
  Outline {
    Text { I'm trying to inject this into the Outline component
      text: "Hi I'm the inner text!"
    }
  }
}
我知道我可以通过使用
Loader
组件并将
sourceComponent
设置为
component
实例来实现这一点,但当我做一些中等复杂的事情(例如可重用对话框)时,我编写函数和引用子组件实例的能力就会受到阻碍。如果组件是在同一个QML文件中实例化的,那么id/函数关系就可以了,我可以直接引用我的文本字段

下面是加载程序的一个示例:

//Outline.qml
Rectangle {
  id : root
  color: 'red'
  property Component content;
  Loader {
    source : root.content
  }
}

//main.qml
Rectangle {
  function getData(){
     return inner.getData(); 
     //ERROR inner is undefined because it is created by the Loader instance.
  }
  ... 
  Outline {
    content: Component {
        TextField {
          id: inner
          function getData(){
            return inner.text || 'none';
          }
        }
    }
  }
}
我希望有更好的处理方法。我知道我可以直接在父级内部构建一个组件,并实现顶级引用,但它不允许我所寻找的控制级别。在这种情况下,最好是组件的“静态”扩展

编辑:这里有一个QML示例说明了这个问题。单击后,输出为:
ReferenceError:getMe未定义

import QtQuick 2.2
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.1

Item {
    id: app
    width: 640
    height: 480

    function accessData(){
        output.text = getMe.text
    }

    Loader {
        width: parent.width
        height: 300
        sourceComponent: Component {
            Rectangle {
                anchors.fill: parent
                color: 'yellow'

                TextField {
                    anchors.centerIn: parent
                    id: getMe
                    placeholderText: 'Input data...'
                }
            }
        }
    }


    Button {
        id: button
        y: app.height - 50
        text: 'get data'
        onClicked: {
            accessData();
        }
    }

    Text {
        y: app.height - button.height
        id: output
        text: 'none'
    }
}

如您所见,当您使用“loader”方法时,sourceComponent不是同一棵树的一部分(您无法访问内部数据)。

这是解决问题的示例的有效方法,稍后请在您的项目中尝试

  • 给你的加载器指定一个id,例如:id:yourLoader,加载器内的矩形是:id:yourRect

  • 将一个字符串属性添加到您的rect,它将保存您的TextField.text

    Loader { id: yourLoader
             width: parent.width
             height: 300
             sourceComponent: Component {
    
         Rectangle {
                     property string yourText : getMe.text
                     id: yourRect
                     anchors.fill: parent
                     color: 'yellow'
    
            TextField {
                anchors.centerIn: parent
                id: getMe
                placeholderText: 'Input data...'
            }
        }
      }
    }
    
  • 我们更改您的函数访问数据

     function accessData(){
                    var rect= yourLoader.item //we get yourRect from the loader 
                     output.text = rect.yourText // yourText <-- getMe.text
    
     } 
    
    函数accessData(){
    var rect=yourLoader.item//我们从加载程序获取yourRect
    
    output.text=rect.yourText//yourText For your
    Outline
    您是否希望支持任何
    组件或具有特定角色的特定对象?我希望支持Outline中的任何组件/项目实例,并对其到达的位置进行一定程度的控制。好的,这就好像您希望创建ow一样n布局类的类型?我有一个想法,它有点长,你必须用C++/QML来访问孩子们并获得通知。如果你想知道这是什么,我可以在以后编写解决方案时发布一个答案。我想我对这个问题的任何解决方案都持开放态度。我显然更喜欢纯QML解决方案,因为这是其中的一部分我选择工具包的原因。我用一个有效的(失败的)选项更新了我的问题例如,基本上,你想创建一个<代码> QQuestItAs/COD>派生C++类,并将其暴露到QML中。然后,当在QML中添加一个元素时,你的类将接收到 ItMeMeXe//Cug信号。你可以使用这个信号来遍历<代码> < <代码> QQuijiT>代码> > >移动、更新、它们、et。c、 …谢谢,这正是我想知道的,如果引用没有失败,这就解决了问题。当我回到项目时,我将用单独的QML文件对此进行一次尝试。这肯定解决了问题,对
    loader.item
    的引用是我所缺少的。