简体   繁体   English

QML:如何将子组件添加到基本组件的子组件,而不是组件的根 object?

[英]QML: How to add children to a base component's child instead of the root object of the component?

I have a base component named MyChart.qml like this我有一个名为 MyChart.qml 的基本组件,如下所示

Item {
    id: root
    
    ChartView {
        id: myChartView
    }
    
    RowLayout {
        id: myToolbar
        }
    }
}

So it's basically a chart with a toolbar underneath it.所以它基本上是一个图表,下面有一个工具栏。 I inherit this component in another component like this:我在另一个组件中继承了这个组件,如下所示:

MyChart {
    *List of Children*
}

Here, how can I add the List of Children to myToolbar instead of root of the MyChart component?在这里,如何将子列表添加到myToolbar而不是 MyChart 组件的

Every item has the property data which is used as default property when you add an object inside another.当您在另一个项目中添加 object 时,每个项目都有用作默认属性的属性data Default property mean if you don't specify any property is assumed to be that one.默认属性意味着如果您未指定任何属性,则假定为该属性。

eg: States has default property states: [], so you can do例如:States 有默认属性 states: [],所以你可以这样做

States {
   State{},
   State{}
}

beacause they all refer to that default property.因为它们都引用了该默认属性。 So, about your example it's enough to expose the default property of the component you want to be the direct father of your component因此,关于您的示例,公开您希望成为组件的直接父亲的组件的默认属性就足够了

MyChart.qml MyChart.qml

Item {
   id: root
   
   default property alias data: myToolbar.data
   
   ChartView {
      id: myChartView
   }
   
   RowLayout {
      id: myToolbar
   }
}

Now any child coponent you use will be added directly inside the rowlayout现在您使用的任何子组件都将直接添加到行布局中

MyChart {
    Rectangle { width: 100; height: 100; color:"red"}
    Rectangle { width: 100; height: 100; color:"skyblue"}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM