[PyQt] Drop to system

Baz Walter bazwal at ftml.net
Thu Sep 9 14:24:21 BST 2010


On 09/09/10 01:17, David Boddie wrote:
> On Wed Sep 8 22:33:17 BST 2010, Hugo Leveille wrote:
>
>> I have looked at the doc but all I could find was drag and drop within the
>> pyqt app itself. What id like to do is the, for exemple, attach an url or
>> whatever needed to an item so that if I drag the item into my desktop, in a
>> explorer window or whatever othe app that support drag drop, it will
>> recongnize it
>>
>> Some if anyone is nice enought to give me a quick exemple or to point me at
>> the specific exemple where it is shown, it would be very nice
>
> It's been a while since I looked at this sort of thing, and the example I
> hoped would help me remember doesn't actually do anything with URLs...
>
> You need to set the URL of the MIME data you create when you start a drag
> operation:
>
> http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qmimedata.html#setUrls
>
> I seem to remember that you have to supply URLs for certain things to work
> on certain systems, and this may mean that you have to save data to a
> temporary file, then set the URL of that file, when filling in the MIME data.
>
> Here is one way to do it that doesn't use a temporary file:
>
>    http://www.diotavelli.net/PyQtWiki/Exporting_a_file_to_other_applications
>
> The Drop Site example in Qt is useful for looking at the MIME types of data
> dragged from other applications. It would be useful if someone ported that
> to PyQt.

as i pointed out in an earlier post, the draggabletext example in the 
pyqt sources can easily be adapted to work with urls: just change 
mimeData.setText to mimeData.setUrls and it will just work :)

anyway, here's a simplified version of the wiki example that doesn't 
write any additional files:

import sys, os
from PyQt4.QtCore import Qt, QMimeData, QUrl
from PyQt4.QtGui import QApplication, QLabel, QDrag


class Window(QLabel):
     def __init__(self):
         QLabel.__init__(self)
         self.setWindowTitle('Drag File Url')
         self.setText(os.path.join(os.getcwd(), __file__))
         self.setMargin(20)

     def mousePressEvent(self, event):
         if event.button() == Qt.LeftButton:
             data = QMimeData()
             data.setUrls([QUrl.fromLocalFile(self.text())])
             drag = QDrag(self)
             drag.setMimeData(data)
             drag.exec_()


if __name__ == "__main__":

     app = QApplication(sys.argv)
     window = Window()
     window.show()

     sys.exit(app.exec_())



More information about the PyQt mailing list