TrungPC
TrungPC

Reputation: 57

Loop in QML when using FileDialog

I have some problem with the loop. I'm trying to load img from browserfile and show others in 3s as image - slide. I used while loop

FileDialog {
    id: fileDialog
    visible: false
    title: "Choose a file"
    property url defaultz: "E:\IMG"
    folder: defaultz
    selectMultiple: true
    nameFilters: [ "Image files (*.jpg *.png *.bmp)", "All files (*)" ]
    onAccepted: {
        console.log("You chose: " + fileDialog.fileUrls)
        console.log(fileDialog.fileUrls.length)
        click.visible = false
        //title.visible = false
        while(i<fileDialog.fileUrls.length){
            loop()
        }

    }

    onRejected: {
        console.log("Canceled")
        fileDialog.visible = false
        click.visible = false
    }

    Component.onCompleted: visible = false
}


Image {
    id: show
    visible: false
    x:0
    y:0
    width: 300
    height: 300
    Timer{
        id: tmr
        interval: 5000
        running: false
        repeat: false
        onTriggered: {
            show.visible = false
        }


    }
}
function loop(){
    show.source = fileDialog.fileUrls[i]
    show.visible = true
    tmr.running = true
    i++
}

When loop() is called, it will run show.source = fileDialog.fileUrls[i] to stopped. After that, show.visible = true and tmr.running will be called.

Someone help me please?

Upvotes: 0

Views: 565

Answers (1)

nayab
nayab

Reputation: 2380

Problem is with while, i value reached to the end before the time triggers.

One solution will be calling the loop on expiry of timer i.e onTriggered and stop the timer after showing all the selected pictures in loop function.

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    property int i:0

    FileDialog {
        id: fileDialog
        visible: false
        title: "Choose a file"
        property url defaultz: "E:\IMG"
        folder: defaultz
        selectMultiple: true
        nameFilters: [ "Image files (*.jpg *.png *.bmp)", "All files (*)" ]
        onAccepted: {
            console.log("You chose: " + fileDialog.fileUrls)
            console.log(fileDialog.fileUrls.length)
            //click.visible = false
            //title.visible = false
//            while(i<fileDialog.fileUrls.length){
//                loop()
//            }
            loop();      // show first picture immediately
            tmr.start(); // start timer after selection

        }

        onRejected: {
            console.log("Canceled")
            fileDialog.visible = false
            //click.visible = false
        }

        Component.onCompleted: {fileDialog.visible = true
        }
    }


    Image {
        id: show
        visible: false
        x:0
        y:0
        width: 300
        height: 300
        Timer{
            id: tmr
            interval: 5000
            running: false
            repeat: true
            onTriggered: {
                show.visible = false
                show.visible = false
                            i++
                            loop()
                            console.log("triggered: " + i)
            }


        }
    }
    function loop(){
        if(i<fileDialog.fileUrls.length)
           {
           show.source = fileDialog.fileUrls[i]
           console.log("showing: " + i + " " + fileDialog.fileUrls[i])
           show.visible = true

           }else
           {
               tmr.stop();  // stop the timer
               console.log("stopped")
           }

    }
}

Upvotes: 0

Related Questions