首页 > 解决方案 > 无法分配 QList到 QString

问题描述

我遇到了这个错误:

Unable to assign QList<QUrl> to QString

当尝试将(从的处理程序获得)的结果直接分配给Python中的标签属性时。drop.urlsDropAreaonDroppedtext

基于this doc,我尝试Qt.resolvedUrl(将类型转换为字符串),如下面的代码所示。但是,它会导致一个空的文本标签。我正在使用的网址以“ 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 的上下文中被问过,我从昨天开始搜索后还没有找到任何这样的现有问题。

标签: qtdrag-and-dropqmlqtquick2

解决方案


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
}

推荐阅读