问题
I'm encountering this error:
Unable to assign QList<QUrl> to QString
when trying to directly assign the result of drop.urls
(obtained from DropArea
's onDropped
handler) to a Label's text
property in Python.
Based on this doc, I tried Qt.resolvedUrl
(to convert the type to a string) as shown in the following code. However, it results in an empty text label. The urls I'm working with start with "file:///".
What am I doing wrong?
import QtQuick.Window 2.2
import QtQuick 2.2
import QtQuick.Controls 2.14
Window {
id: root
visible: true
width: 640
height: 480
title: "Drop Test"
property var attachments: "empty"
DropArea {
id: dropArea;
anchors.fill: parent
onEntered: {
root.color = "gray";
console.log("You entered drop area")
drag.accept (Qt.LinkAction);
}
onDropped: {
console.log("You dropped " + drop.urls)
attachments = Qt.resolvedUrl(drop.urls)
}
}
Label {
id: mLableId
text: attachments
}
}
Assigning a URL to a string seems like such an obvious question, but if it has already been asked in the context of Python and Qt Quick, I have not find any such existing questions after searching since yesterday.
回答1:
urls is a list of url so you will have to iterate and concatenate:
onDropped: {
console.log("You dropped " + drop.urls)
var str = ""
for(var i in drop.urls){
var url = drop.urls[i]
str += Qt.resolvedUrl(url)
}
attachments = str
}
来源:https://stackoverflow.com/questions/59723408/unable-to-assign-qlistqurl-to-qstring