-1

Ubuntu 16.04 Qt, .qml

Hi, I am trying to loop over Urls received from user by calling

console.log(fileDialogDCM.fileUrls.count)

after user successfully selects files from directory in FileDialog (to get their maximum number). However, I received "undefined" instead. Object fileDialogDCM.fileUrls should be of type QList<QUrl>. I can access individual Url by calling

console.log(fileDialogDCM.fileUrls[i])

so I expect that considered QList is not empty. Consider me a noob in qml, what am I doing wrong? Here is minimum not working example:

FileDialog {
    id: fileDialogDCM
    selectMultiple: true
}

Button {
    id: loadDCM
    text: qsTr("Load DCM")
    property var aListOfPlans: fileDialogDCM.fileUrls
    onClicked: {
        for(var i=0; i<4; i++) {
          console.log(aListOfPlans[i])
        } //writes URLs of first 4 selected plans
        console.log(aListOfPlans.count) //gives undefined 
    }
}

Thanks a lot

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
tyraell
  • 1
  • 4

2 Answers2

0

I wonder what do you expect to get in your code. Don't you think you have to open FileDialog before printing selected urls out? Also there is no list.count property at all.

I guess that should be as following:

FileDialog {
    id: fileDialogDCM
    selectMultiple: true
    onAccepted: {
        for(var i = 0;i < fileDialogDCM.fileUrls.length;i ++)
        {
            console.log(fileDialogDCM.fileUrls[i]);
        }
    }
}

Button {
    id: loadDCM
    text: qsTr("Load DCM")
    onClicked: {
        fileDialogDCM.visible = true;
    }
}

Please refer to Qt's excellent documentation/examples instead of inventing/implementing your idea of the right approach.

folibis
  • 12,048
  • 6
  • 54
  • 97
  • If I run the above "FileDialog" documentation example I also get "qml: You chose: undefined". I've been using Qt for quite a while, but not in python or using the QML/Quick things. These "excellent" examples are quite confusing for people trying to get the basics going, as many times we hit these walls of unexpected output. Or it simply doesn't work. The example you linked to also gives an error on the folder variable, saying "Cannot assign to non-existent property "folder"". – Casper B. Hansen Nov 06 '21 at 10:59
0

Sure I am calling open on FileDialog but I didnt include it in my example considering it obvious.

MenuItem {
            text: qsTr("Import DCM")
            onTriggered: {
                fileDialogDCM.open()
                console.log("[INFO]     Import DCM triggered")
            }
        }

However, I found solution (or realized what real problem was). I was using .count (gives undefined) instead of .legth (gives number)

tyraell
  • 1
  • 4
  • Nice, but you have not post your comment as answer. In the future, always provide all the relevant code and try to describe the problem as clearly as possible. – folibis Jul 25 '18 at 08:58