Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I load multiple images to be accessed by a Canvas as an array in QML



  • This is what I have tried doing:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        width: 1024
        height: 768
        visible: true
        title: qsTr("Hello World")
        Canvas{
            id: mycanvas
            anchors.centerIn: parent
            width: 500
            height: 500
            Component.onCompleted:  {
                var myfiles = ["/images/test0.png","/images/test1.png","/images/test2.png","/images/test3.png"]
                var oneEl = ""
                for (var i=0;i<4;i++){
                    oneEl = myfiles[i]
                    loadImage(mycanvas.oneEl);
                }
            }
    
            //This part I can play with later
            onPaint: {
                var ctx = getContext("2d");
                ctx.fillStyle = Qt.rgba(1, 0, 0, 1);
                ctx.drawImage()
            }
        }
    
    }
    
    

    When I actually need more images I can change the way I access their names. I'm thinking of converting a number to a hexadecimal string to access the files which I will have to figure out how to do



  • @AI_Messiah said in How do I load multiple images to be accessed by a Canvas as an array in QML:

    loadImage(mycanvas.oneEl);

    Shouldn't this be loadImage(oneEl)? oneEl is not a property of mycanvas.

    Edit: isImageLoaded will tell you if its loaded. https://doc.qt.io/qt-5/qml-qtquick-canvas.html#loadImage-method


Log in to reply