使用FileDialog时在QML中循环

时间:2018-08-03 11:30:47

标签: qt qml filedialog

循环有问题。我正在尝试从browserfile加载img并将其他人以3s的形式显示为图片-幻灯片。我使用了while循环

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++
}

调用loop()时,它将运行show.source = fileDialog.fileUrls[i]停止。之后,将调用show.visible = truetmr.running

有人帮我吗?

1 个答案:

答案 0 :(得分:0)

问题while出现,我的值在时间触发之前达到了终点。

一种解决方案是在计时器onTriggered到期时调用循环,并在loop函数中显示所有选定的图片后停止计时器。

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")
           }

    }
}