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. How to make a "for" loop in QML?
Forum Updated to NodeBB v4.3 + New Features

How to make a "for" loop in QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 16.5k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by Mikeeeeee
    #1

    Hi!
    How to make a "for" loop in QML?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      QML is based on JavaScript, so the same way you would in JavaScript.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        I did so:

        for (var i = 0; i < 9; i++) 
        

        I get errors:
        qrc:/main.qml:12 Unexpected token `for'

        qrc:/main.qml:12 Unexpected token `var'

        qrc:/main.qml:12 Expected token `,'

        Gojir4G 1 Reply Last reply
        0
        • M Mikeeeeee

          I did so:

          for (var i = 0; i < 9; i++) 
          

          I get errors:
          qrc:/main.qml:12 Unexpected token `for'

          qrc:/main.qml:12 Unexpected token `var'

          qrc:/main.qml:12 Expected token `,'

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @Mikeeeeee Syntax is correct, your code is probably located at the wrong place. You need to be in a function to execute some code, or wrap it inside {} and use return. Can you show us the code ?

          1 Reply Last reply
          4
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #5
            import QtQuick 2.12
            import QtQuick.Window 2.12
            import QtLocation 5.12
            import QtPositioning 5.12
            
            Window {
                visible: true
                    width: 480
                    height: 720
                    title: qsTr("Map OSM")
            
                    for (var i = 0; i < 9; i++)  {
                        console.log(i)
                        i++
                    }
            
                     Connections
                     {
                         target: houseNumber 
                     }
            
                    Loader
                    {
                        id: loader
                        anchors.fill: parent
                        //source: "map.qml"
                    }
            }
            
            
            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by
              #6

              it is work

              import QtQuick 2.12
              import QtQuick.Window 2.12
              import QtLocation 5.12
              import QtPositioning 5.12
               
              Window {
                  visible: true
                      width: 480
                      height: 720
                      title: qsTr("Map OSM")
               
                      /* С помощью объекта Connections
                        * Устанавливаем соединение с классом ядра приложения
                        * */
                       Connections
                       {
                           target: houseNumber // Указываем целевое соединение
                           /* Объявляем и реализуем функцию, как параметр
                            * объекта и с имененем похожим на название сигнала
                            * Разница в том, что добавляем в начале on и далее пишем
                            * с заглавной буквы
                            * */
                          /* onSendToQml: {
                               labelCount.text = count // Устанавливаем счётчик в текстовый лейбл
                           }*/
                       }
               
                      Loader
                      {
                          id: loader
                          anchors.fill: parent
                          //source: "map.qml"
                      }
               
                      Component.onCompleted: {
                          for (var i = 0; i < 9; i++)  {
                              console.log(i)
                              i++
                          }
                      }
              }
              
              1 Reply Last reply
              2

              • Login

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