Unable to assign QList<QUrl> to QString

雨燕双飞 提交于 2020-01-16 08:14:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!