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. Can I use 'yield' from QML/JS or What alternative is its?
Forum Update on Monday, May 27th 2025

Can I use 'yield' from QML/JS or What alternative is its?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 748 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.
  • I Offline
    I Offline
    Ibrahim
    wrote on last edited by
    #1

    Hi, I have these example codes:

    // JsFile.js
    ...
    var myArray = [5, 4, 1, 7, 9, 1, 2, 4, 3, 7];
    ...
    function* getIndex() {
      yield* myArray;
    }
    
    // MyImage.qml
    import './js/JsFile.js' as JsFile
    Image {
      id: myImage
    
      // Only ES6:
      property int imageIndex: JsFile.getIndex().next().value // not working! (Error message: TypeError: Cannot read property 'imageIndex' of undefined)
      source: imageIndex + '.png' // Example: 1.png, 2.png, 3.png ...
    }
    
    // main.qml
    ...
    Repeater {
      model: 10 // Length of myArray is 10
    
      MyImage { }
    }
    ...
    

    I know that QML doesn't support ES6 but I just tried :)
    I have an array like its and I use Repeater, so I need to use Js generators but I can't. What is your suggestion for this problem? Thanks.

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

      @Ibrahim hi

      import QtQuick 2.0
      import QtQuick.Controls 2.0
      import QtQuick.Layouts 1.3
      
      Item{
          RowLayout{
              id:root
              anchors.fill: parent
              property variant myArray : [0,2,3,0]
              Repeater{
                  id:r
                  model : 4
                  Image{
      
                      //  source : "img-" + index + ".png"   OR
      
                      source : "img-" + root.myArray[index] + ".png"
                      Layout.preferredWidth:50
                      Layout.preferredHeight:50
                  }
              }
          }
      }
      
      

      maybe this can help you

      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