Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Accessing children in the repeater

Accessing children in the repeater

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gongdori
    wrote on last edited by
    #1

    Hi all,

    I'm new in Qt and I need your help. I instantiated 8 buttons using repeater and want to call a function whenever any of the buttons are pressed and pass the index as a parameter. I tried to declare aliases of the buttons using itemAt() and call a function when onClicked() occurs, but an error was thrown. It seems qt does not allow it...
    How can I make it work?
    Thanks,

    gongdori

    Repeater {
                       id: repeater_ae_set
                       model: 8
                       Button {
                           id: button_ae_set
                           text: qsTr("Set")
                           Layout.fillHeight: true
                           Layout.fillWidth: true
    
                       }
    
                   }
    
    
    ODБOïO 1 Reply Last reply
    0
    • G gongdori

      Hi all,

      I'm new in Qt and I need your help. I instantiated 8 buttons using repeater and want to call a function whenever any of the buttons are pressed and pass the index as a parameter. I tried to declare aliases of the buttons using itemAt() and call a function when onClicked() occurs, but an error was thrown. It seems qt does not allow it...
      How can I make it work?
      Thanks,

      gongdori

      Repeater {
                         id: repeater_ae_set
                         model: 8
                         Button {
                             id: button_ae_set
                             text: qsTr("Set")
                             Layout.fillHeight: true
                             Layout.fillWidth: true
      
                         }
      
                     }
      
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      Hi @gongdori
      Property index is avalable inside Buttons (Repeater's children) .

      For exemple you can create a signal or javascript function and call it inside buttons onClick

      import QtQuick 2.0
      import QtQuick.Controls 2.0
      import QtQuick.Layouts 1.3
      
      Item{
          id:root
          signal clickIndexSignal(var i) // SIGNAL
          onClickIndexSignal:{ // SIGNAL handler
              console.log("button " + i + " clicked!")
          }
      
          function clickIndexFunction(i){
              return(console.log("button " + i + " clicked!"))
          }
      
          RowLayout{
                     anchors.fill: parent
      
              Repeater{
                  model : 8
                  Button{
                      Layout.fillWidth: true
                      Layout.fillHeight: true
                      text: "button" + index
                      onClicked:{
                          clickIndexSignal(index)
                          clickIndexFunction(index)
                      }
                  }
              }
          }
      }
      

      LA

      1 Reply Last reply
      1
      • G Offline
        G Offline
        gongdori
        wrote on last edited by
        #3

        Hi @LeLev
        Thank you for your help. I tried to do what you recommended but I got an error saying "functions are not supported in Qt quick UI form (M222). It seems I need to setup alias for the buttons, but I could not do it. If you know how to do it, can you let me know please?
        Thanks,

        Gongdori

        1 Reply Last reply
        0
        • ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          @gongdori hi,
          In fact functions are not supported in Qt quick UI form. Sorry but i have never used UI files son can't help a lot with this.

          You can create your reusable component, call it ButtonRow.qml

          // ButtonRow.qml (same code)

          import QtQuick 2.0
          import QtQuick.Controls 2.0
          import QtQuick.Layouts 1.3
          
          Item{
              id:root
              signal clickIndexSignal(var i) // SIGNAL
              onClickIndexSignal:{ // SIGNAL handler
                  console.log("button " + i + " clicked!")
              }
          
              function clickIndexFunction(i){
                  return(console.log("button " + i + " clicked!"))
              }
          
              RowLayout{
                         anchors.fill: parent
          
                  Repeater{
                      model : 8
                      Button{
                          Layout.fillWidth: true
                          Layout.fillHeight: true
                          text: "button" + index
                          onClicked:{
                              clickIndexSignal(index)
                              clickIndexFunction(index)
                          }
                      }
                  }
              }
          }
          

          Then you can use that ButtonRow like this (in another .qml) :

          ButtonRow{
           width: parent.width
           height : parent.height*0.2
          }
          

          If you want to create/instanciate a ButtonRow inside your ui.qml file, then use Designe mode,
          on the top left Corner of Qt Creator you should see ButtonRow component that you have created.

          I hope this can help you.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gongdori
            wrote on last edited by
            #5

            Hi @LeLev,

            Thank you again for your help. I'll try ButtonRow.qml soon! Out of curiosity, is there any other way to do the design without using UI form? I wonder what options I have.
            Have a nice day.

            Gongdori

            ODБOïO 1 Reply Last reply
            1
            • G gongdori

              Hi @LeLev,

              Thank you again for your help. I'll try ButtonRow.qml soon! Out of curiosity, is there any other way to do the design without using UI form? I wonder what options I have.
              Have a nice day.

              Gongdori

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by
              #6

              @gongdori You are welcome!

              @gongdori said in Accessing children in the repeater:

              is there any other way to do the design without using UI form?

              From what i understand, UIs let us separate logic from visual aspect , but i don't do that, i only use .qml files never *.ui.qml

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved