[PyQt] Drop Site example

Hans-Peter Jansen hpj at urpla.net
Fri Sep 10 15:54:03 BST 2010


On Friday 10 September 2010, 01:35:24 David Boddie wrote:
> On Fri Sep 10 00:04:27 BST 2010, Hans-Peter Jansen wrote:
> > here's a PyQt version of the drop site example, that you might want to
> > add to the PyQt examples. Since modularity is a good idea generally,
> > I've kept the modules organization, but renounced supporting
> > translations.
> >
> > Has anybody an idea, which linux app is able to drop real images
> > similar as to what was done for the screenshot in the Qt docu?
> >
> > http://doc.qt.nokia.com/4.6/draganddrop-dropsite.html
>
> The Delayed Encoding example in Qt's examples directory will send the
> image data in a drag and drop operation.

Thanks, David. Phil, here's the fix for the broken QImage processing:

diff -up dropsite/droparea.py dropsite.fixed/droparea.py
--- dropsite/droparea.py	2010-09-10 11:44:43.641400431 +0200
+++ dropsite.fixed/droparea.py	2010-09-10 11:41:22.005311858 +0200
@@ -34,7 +34,10 @@ class DropArea(QtGui.QLabel):
     def dropEvent(self, event):
         mimeData = event.mimeData()
         if mimeData.hasImage():
-            self.setPixmap(mimeData.imageData())
+            # mimeData.imageData() is a QImage wrapped by in a QVariant.
+            # Thanks to sip's QVariant API Version 2, established in
+            # dropsite.py we just need to convert the QImage to a QPixmap.
+            self.setPixmap(QtGui.QPixmap(mimeData.imageData()))
         elif mimeData.hasHtml():
             self.setText(mimeData.html())
             self.setTextFormat(QtCore.Qt.RichText)
diff -up dropsite/dropsite.py dropsite.fixed/dropsite.py
--- dropsite/dropsite.py	2010-09-10 11:44:50.369627362 +0200
+++ dropsite.fixed/dropsite.py	2010-09-10 11:39:35.957322907 +0200
@@ -8,6 +8,13 @@
 ###########################################################################
 
 import sys
+
+# This is only needed for Python v2 but is harmless for Python v3
+# in order to access the QImage in DropArea.dropEvent() without 
+# any explicit conversions (aka .toPyObject()).
+import sip
+sip.setapi('QVariant', 2)
+
 from PyQt4 import QtCore, QtGui
 
 from dropsitewindow import DropSiteWindow

> There's also a hidden example in 
> the doc/src/snippets/separations directory that does a similar thing.

Nice. Let's see, how that one does with python. Stay tuned..

> I think the reason why many applications don't send image data directly
> is that it can involve a lot of data. The Delayed Encoding example still
> sends the image data, but it only generates it when the drag and drop
> operation is complete and the application has committed to sending it.

Yes, of course. I didn't find something in order to test this, and 
consequently it failed..

My favorite motto in this respect:

	Code that isn't tested won't work.

Sincerely,
Pete
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dropsite-fix.diff
Type: text/x-diff
Size: 1397 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100910/02864729/attachment.diff>


More information about the PyQt mailing list