Qml Property Binding problem?



  • I want to get image files by FileDialog, and my codes are as follows with Qt 5.9:

    Rectangle {
    visible: true
    width: 640
    height: 480

    focus:true
    property var listUrls:["file:///C:/Qt/QtLearn/图片"]
    
    MainForm {
        anchors.fill: parent
        mouseArea.onClicked: {
            console.log("onClicked listUrls Image Num:", listUrls.length)
            fileDialog.open()
            console.log("onClicked open listUrls Image Num:", listUrls.length)
            console.log("onClicked open fileDialog.fileUrls Image Num:", fileDialog.fileUrls.length)
    
        }
    }
    
    
    FileDialog {
        id: fileDialog
        title: "please choose a file"
        nameFilters: ["Image File(*.jpg *.png *.gif)"]
        folder: "file:///C:/Qt/QtLearn/Images"
        selectMultiple: true
    
        onAccepted: {
            listUrls =fileDialog.fileUrls
            console.log("onAccepted listUrls Image Num:", listUrls.length)
        }
        onRejected: {
            console.log("onRejected listUrls Image Num:", listUrls.length)
            console.log("nRejected fileDialog.fileUrls Image Num:", fileDialog.fileUrls.length)
        }
    }
    

    }

    When I first click mouseArea,and select 3 images , the outputs are ok:
    qml: onClicked listUrls Image Num: 1
    qml: onClicked open listUrls Image Num: 1
    qml: onClicked open fileDialog.fileUrls Image Num: 0
    qml: onAccepted listUrls Image Num: 3

    But when I click mouseArea again ,and select 2 images,the outputs are as follows:
    qml: onClicked listUrls Image Num: 3
    qml: onClicked open listUrls Image Num: 0
    qml: onClicked open fileDialog.fileUrls Image Num: 0
    qml: onAccepted listUrls Image Num: 2

    Can anyone tell me why listUrls is set to 0?Property listUrls is bound with fileDialog.fileUrls ?Does assigning a property value (using the equals sign "listUrls =fileDialog.fileUrls") create a property binding?



  • @yezhiyun No it doesn't, on the contrary, it breaks a possible existing binding. The value is just set. You have to use Qt.binding() if you want to create a binding dynamically (in a javascript function).



  • This post is deleted!


  • @Eeli-K

    Yeah,it doesn't,but if i declare listUrls to be string type,when I click mouseArea ,open fileDialog and select 2 images at the second time,the outputs will be :
    qml: onClicked listUrls Image Num: 3
    qml: onClicked open listUrls Image Num: none zero!!!!(Right number that I think it should be)
    qml: onClicked open fileDialog.fileUrls Image Num: 0
    qml: onAccepted listUrls Image Num: 2.
    It's very strange.

    In Qt install Example systemdialogs.pro/FileDialogs.qml,there are some property binding codes like:
    Label {
    text: "<b>chosen files:</b> " + fileDialog.fileUrls
    }

    But when I open FileDialog and select images ,choose reject button,In the onRejected function,I output fileDialog.fileUrls,the outputs are images I choose,but the label above doesn't update,the binding seems fail.If I choose accept those Image ,the binding will work.these make me confused.



  • @yezhiyun said in Qml Property Binding problem?:

    But when I open FileDialog and select images ,choose reject button,In the onRejected function,I output fileDialog.fileUrls,the outputs are images I choose,but the label above doesn't update,the binding seems fail.If I choose accept those Image ,the binding will work.these make me confused.

    Maybe it sends the fileUrlsChanged signal only when the dialog is accepted. It makes sense because the program should react only when the user accepts it. If the user rejects (cancels) it, the program should behave as if the user did nothing, not even opened the dialog. If that's a problem for you there's another FileDialog in Qt.labs.platform which has 'currentFiles' property.


Log in to reply
 

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