Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qml Property Binding problem?
Qt 6.11 is out! See what's new in the release blog

Qml Property Binding problem?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yezhiyun
    wrote on last edited by
    #1

    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?

    E 1 Reply Last reply
    0
    • Y yezhiyun

      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?

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @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).

      Y 1 Reply Last reply
      1
      • Y Offline
        Y Offline
        yezhiyun
        wrote on last edited by yezhiyun
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • E Eeli K

          @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).

          Y Offline
          Y Offline
          yezhiyun
          wrote on last edited by
          #4

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

          E 1 Reply Last reply
          0
          • Y yezhiyun

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

            E Offline
            E Offline
            Eeli K
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved