简体   繁体   English

Qml / Qt 创建者:如何多次“生成”/创建自定义组件

[英]Qml / Qt creator: How to “spawn”/create a custom component multiple times

Trying to make a UI for a program in Qt creator.试图为 Qt creator 中的程序制作 UI。

I got a scroll view where I want to place a custom component (a rectangle).我有一个滚动视图,我想在其中放置一个自定义组件(一个矩形)。 I can manually put the component in there multiple times and scroll up and down so got that working.我可以多次手动将组件放入其中并上下滚动以使其正常工作。

My problem is that I want to generate the component automatically when the user presses a button.我的问题是我想在用户按下按钮时自动生成组件。

So my question is how to "generate/spawn/create" a custom component into a scroll view when the user presses a button所以我的问题是当用户按下按钮时如何将自定义组件“生成/生成/创建”到滚动视图中

So far I've tried with loaders and calling the custom component directly inside an onClicked function but this does not seem to work.到目前为止,我已经尝试使用加载器并直接在 onClicked function 内调用自定义组件,但这似乎不起作用。

var component = Qt.createComponent("CustomComponent.qml"); var 组件 = Qt.createComponent("CustomComponent.qml");

 onClicked: { component.createObject(column);

I am trying to create the component inside a column with the id column我正在尝试在具有 id 列的列内创建组件

you can compare what I am trying to achieve with a social media feed.您可以将我想要实现的目标与社交媒体源进行比较。 Where I got a list that is scrollable and I want to put components(atm just a rectangle) in the list.我有一个可滚动的列表,我想将组件(atm 只是一个矩形)放在列表中。

You can use Repeater for that:您可以为此使用中继器

Repeater {
    id: repeater
    model: 0

    CustomComponent {}
}

Button {
    onClicked: {
        repeater.model += 1;
    }
}

Repeaters can be used in positioning items(Row, Column, Grid) and layouts.中继器可用于定位项目(行、列、网格)和布局。 Also it is possible use a list as a model.也可以使用列表作为 model。 So in that case you can reach model data(model[index]) with modelData and index with index in your custom component.因此,在这种情况下,您可以在自定义组件中使用modelDataindex达到 model data(model[index]) 和 index 。

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

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