qt - 无法将 QList<QUrl> 分配给 QString

标签 qt drag-and-drop qml qtquick2

我遇到了这个错误:

Unable to assign QList<QUrl> to QString

当尝试直接分配 drop.urls 的结果时(从DropAreaonDropped 处理程序获得) 到 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/

相关文章:

c++ - 将 QStandardItemModel 从 C++ 传递到 QtQuick/QML TableView 并显示它

c++ - Qt 5.0 Json编码

qt - Qt 如何在矩形周围绘制边框?

c++ - 如何在我的应用程序中嵌入自定义字体

json - 在Mac Catalyst中运行的iOS应用程序中的DropInteraction中接收JSON文件

c++ - 将 Qt3D 用于巨大的地形模型?

QtQuick 2 - 侧面板示例

Qt:使用拖放重新排序 ListView 项目

android - 调整放置区的大小以消除气泡

xml - 如何将二进制代码转换为 qml 中的 png 图像?