我遇到了这个错误:
Unable to assign QList<QUrl> to QString
当尝试直接分配 drop.urls
的结果时(从DropArea
的onDropped
处理程序获得) 到 Python 中 Label 的 text
属性。
基于 this doc ,我尝试了 Qt.resolvedUrl
(将类型转换为字符串),如下面的代码所示。但是,它会产生一个空的文本标签。我正在使用的 url 以“file:///”开头。
我做错了什么?
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
}
}
将 URL 分配给字符串似乎是一个显而易见的问题,但如果在 Python 和 Qt Quick 的上下文中已经有人问过它,我从昨天开始搜索后没有发现任何此类现有问题。
最佳答案
urls 是一个 url 列表,所以你必须迭代和连接:
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
}
关于qt - 无法将 QList<QUrl> 分配给 QString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59723408/