Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get drag and drop relative URL?



  • Hello, I'm making a file upload tool where users can drag and drop files onto the application. The problem is that I need to know what folder they are dragging the files from, and I don't see anyway to find this out... I tried checking drop.source, but it was null.

    So to be clear... I need a way to either:

    • Get the path to the folder we are dragging the files from.
    • Get the URLs as relative URLs from the folder we are dragging them from.

    It this possible? Here is a minimal version of the code I have.

    Window {
        width: 640
        height: 480
        
        title: 'Uploader'
        visible: true
        
        DropArea{
            id: dropArea
            anchors.fill: parent
            onDropped: {
                for(var i in drop.urls)
                {
                    // This holds the absolute path... I need either the relative path from the folder we are dragging from or I need to know what the path of the folder we are dragging from is.
                    console.log(drop.urls[i]);
                }
            }
        }
    }
    

    P.S...
    I also I have a problem where I can't drop files into a DropArea if the application is started using Qt Creator "Run" and/or "Debug" functions, but I can if I launch the application directly through it's compiled EXE. I get a not allowed cursor when I try, and nothing happens if I try to drop the files anyways. Does anyone know how to fix this? It's making debugging... annoying.



  • You will have to feed the urls to a c++ function that accepts QUrl and dissect your paths there.



  • You will have to feed the urls to a c++ function that accepts QUrl and dissect your paths there.



  • Thanks @fcarney I already did that, was just hoping there was a QML way...

    Do you have any idea why I can't drop files when running the program using Qt Creator's "Run" function?



  • @KidTrent said in How to get drag and drop relative URL?:

    was just hoping there was a QML way...

    I created a set of QUrl related objects I exposed to QML for doing some processing.

    I am not sure what is happening with the not being able to drop. I have not seen that issue during my testing. Are you using drop keys of any kind? Are there some weird permissions going on?

    Maybe show us a barebones code that shows this problem.



  • I opened a topic about it last night on Stackoverflow. Here's the article...

    https://stackoverflow.com/questions/67797636/qml-droparea-external-files-fails?noredirect=1#comment119835299_67797636

    Here's a summary if you don't want to go to stackoverflow :P...

    I'm trying to make it so I can drag and drop external files (from Explorer) into a region of my application and it will list some information about those files. The problem is that every time I drop them into a DropArea, the application says it's not allowed for some reason.

    I found out that if I run the application directly through the compiled EXE, instead of through Qt Creators "Run (Ctrl+R)" functionality it works as expected! That is to say, I'm able to drop files on the DropArea.... I'm just not sure why it doesn't work if it's ran using Qt Creator... Debugging will be very annoying like this, does anyone have any thoughts as to how I can make it work via Qt Creator too?

    Here is a video of what happens. https://puu.sh/HLz3S/39e98ed95d.mp4

    I'm running the following specs:

    • Windows 10 Pro
    • Qt 5.15.2 (Shown in video)
    • Qt Creator 4.15 (Shown in video)

    This is the code... everything else is default from a new Qt Quick application...

    import QtQuick 2.9
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.3
    import QtQuick.Layouts 1.3
    
    Window {
        width: 640
        height: 480
    
        title: 'Accordion'
        visible: true
    
        DropArea {
            anchors.fill: parent
            onDropped: {
                console.log("Dropped");
            }
        }
    }
    


  • import QtQuick 2.15
    import QtQuick.Window 2.15
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Generic Drag Test")
    
        DropArea {
            anchors.fill: parent
    
            onDropped: {
                console.log("Dropped");
            }
        }
    }
    

    Tested on windows 10 and ubuntu 18.04. It drops fine when running application from qt creator. I used the 5.15.2 kit in both instances. I dragged a text file from the desktop in both cases.



  • @fcarney Yah, from everything I've read (hours worth) everyone else is able to do it just fine too... Not sure why for whatever reason I cannot...



  • It might be a privilege issue. Are you running creator with elevated privileges?



  • I tried starting it by right clicking and clicking "Run as Administrator", had the same issue.... It's very annoying, I'm having to compile the code, and run the exe separately each time to test changes :/.



  • If anyone else experiences this... I found out this ONLY happens if Qt Creator is Run as Admin... Not sure why this is the case, I'd expect it the other way around. So much time wasted...



  • @KidTrent That link I provided explains why.



  • @fcarney Oh shoot, didn't see that that was a link. Thanks!


Log in to reply