D
D
dmitry_maslov2018-05-18 01:17:44
Qt
dmitry_maslov, 2018-05-18 01:17:44

QML. repeater. How to find out the Index of each Repeater element?

A certain number of buttons are created using Repeater(ListModel). How to access a specific button and find out its parameters (X, Y)?
main.qml

Repeater {
    model: buttons

    buttons{

    }

    ListModel { // модель для кнопок 
        id: buttons
        ListElement { model_button_text: "1"; }
        ListElement { model_button_text: "2"; }

buttons.qml
Button{
id: btn
Layout.preferredWidth: Screen.pixelDensity * 18
Layout.preferredHeight: Screen.pixelDensity * 21
Text{
    anchors.centerIn: parent
    text:model_button_text
    font.pixelSize: Screen.pixelDensity * 5
    color: "#056699"
    font.bold: true
}

background: Rectangle{
    color:"black"
    //radius: 2
    border.width: 2
    border.color: "#056699"
}}

When I click on another button, I need to refer to a specific button (created by Repeater) and get its X and Y (coordinates).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eeiaao, 2018-06-11
@eeiaao

For example

SomeRoot {
    id: root

    Repeater {
        model: buttons
        delegate: SomeDelegate {}
   }

Component.onCompleted: {
   [].slice.call(root.children, 0, root.children.length - 1).forEach(function (item) { console.warn(item.x) });   
}

Or like this:
Repeater {
    id: repeater

    model: buttons
    delegate: SomeDelegate {}

    Component.onCompleted: {
        for (var i = 0; i < repeater.count; ++i)
            console.warn(repeater.itemAt(i).x);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question