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



  • 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.



  • @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.