How to acquire absolute filepath of drag-and-dropped file

1,003 views
Skip to first unread message

Fredrik Averpil

unread,
Mar 13, 2015, 4:48:10 AM3/13/15
to python_in...@googlegroups.com
Hi,

I've created a custom QLabel class which allows dropping a file onto it. But I'm having issues with fetching the filepath of the dropped file when dropped from anything but a Windows drive letter. So for example if you drag and drop a file from a network share (which has not been mapped to drive letter), I'm not getting the absolute full path. Also, on Mac OS X my code does not seem to work at all.

I'm totally new to the whole drag and drop concept and I feel I haven't had the time to read up on this prior to dig into code. So I'm a little lost on the concept side of things.

Do I have to check for the filepath of the dropped file in several different ways? This seems somewhat odd...

Any tips or ideas on how to deal with this are very much welcome.

Cheers,
Fredrik


This is the code I have currently:

class DropableQLabel(QtGui.QLabel):
    def __init__(self, parent):
        super(DropableQLabel, self).__init__(parent)

        self.skalman = parent.skalman
        self.skalman.browser = parent

        self.setFixedWidth(300)
        self.setFixedHeight(169)
        self.setFrameShape(QtGui.QFrame.Box)
        self.setAlignment(QtCore.Qt.AlignCenter)
        self.setAcceptDrops(True)
        

    def dragEnterEvent(self, event):
        print 'Entered droppable area'
        if event.mimeData().hasUrls():
            event.acceptProposedAction()
        else:
            super(DropableQLabel, self).dragEnterEvent(event)


    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            for url in event.mimeData().urls():
                print url.path() # this is not always an absolute filepath
                

            event.acceptProposedAction()
        else:
            super(DropableQLabel,self).dropEvent(event)

Justin Israel

unread,
Mar 13, 2015, 6:38:26 AM3/13/15
to python_in...@googlegroups.com

I don't recall there being too much variation in the way you retrieve the path of a dropped file. You just check for the urls, and for text. It's up to the underlying platform specific code to deliver the mime info as part of its drop.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAD%3DwhWP6rn%2B6-A8dd4p1TA6gRu3FSDw%2Bc5vWsfJ1gmfOUUWdmQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Fredrik Averpil

unread,
Mar 13, 2015, 7:55:11 AM3/13/15
to python_in...@googlegroups.com
Well, that's what I thought too, from reading the docs.

But when dragging a file from \\100.0.0.1\share\folder\file.txt onto my droppable widget will result in the url: /share/folder/file.txt ... with the whole hostname/ipaddr omitted from the url. A bit confusing...

// Fredrik



Justin Israel

unread,
Mar 13, 2015, 4:40:18 PM3/13/15
to python_in...@googlegroups.com
What happens when you drag and drop that same thing onto a cmd terminal? I might be wrong, but my guess is that it is the actual mime data being delivered to your application. 

Fredrik Averpil

unread,
Mar 13, 2015, 5:03:43 PM3/13/15
to python_in...@googlegroups.com
Hi Justin,

When I drop a file from \100.0.0.1\share\folder\file.txt  onto a cmd window (Windows 7), the whole path is entered in the cmd window.

// Fredrik


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2CGJve8MCjqx8S%3DEHemajOWFysYtMj4Y24uL5j3YzCwQ%40mail.gmail.com.

Justin Israel

unread,
Mar 13, 2015, 5:35:03 PM3/13/15
to python_in...@googlegroups.com
Hmm, then I guess the windows specific logic in Qt is doing something :-/

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAD%3DwhWP3knkKCWDH_m08fAUzcZgh3wF4OhYFO9khXo7p6%2B7H5Q%40mail.gmail.com.

Fredrik Averpil

unread,
Mar 14, 2015, 5:45:31 AM3/14/15
to python_in...@googlegroups.com
I think I figured it out.

When dragging and dropping a file from a Windows UNC path, you need to also query url.host() to get the host name/ipaddr.

So with my (previously posted) code, this finally catches the correct filepath of such a file.
def dropEvent(self, event):
    if event.mimeData().hasUrls():
        for url in
 event.mimeData().urls():
            print url.host()
            print url.path()
            return url.host()+url.path() # absolute filepath for UNC/Windows

        event.acceptProposedAction()
    else:
        super(DropableQLabel,self).dropEvent(event)
Haven't tried this on OSX yet.

// Fredrik



Fredrik Averpil

unread,
Mar 14, 2015, 5:52:02 AM3/14/15
to python_in...@googlegroups.com
Uhh... not really sure why I was bothering with all of this when there was this: url.toLocalFile()
:)

That seems to work for all kinds of paths.

// Fredrik


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

johan Borgström

unread,
Sep 4, 2015, 12:05:39 PM9/4/15
to Python Programming for Autodesk Maya
Hi Fredrik,

Did you try this on  OS X Yosemite?
url.toLocalFile() returns '/.file/id=1234567.12345678'. It seems to be a bug in Qt. Is there a known workaround in PySide to get a usable file path?

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages