[PyQt] Drag and Drop

Reinaldo de Carvalho reinaldoc at gmail.com
Tue May 6 15:03:51 BST 2008


I'm trying to do dragdrop work but dont have success. In the example
below the only print is ListView.dragEnterEvent.

Any suggestion?

-- 
Reinaldo Carvalho
http://korreio.sf.net



#!/usr/bin/env python

from qt import *

class ListView(QListView) :

   def __init__(self, parent = None, name = None) :
      QListView.__init__(self, parent, name)

      self.connect(self, SIGNAL("dropped(QDropEvent *)"), self.itemDropped)


   def dragEnterEvent(self, qDragEnterEvent) :
      print "ListView.dragEnterEvent"
      qDragEnterEvent.accept(1)

   def dragObject(self) :
      item = self.currentItem()
      if (not item) :
         return

      itemName = str(item.text(0))
      d = QTextDrag(itemName, self)
      return d


   def itemDropped(self, qDropEvent) :
      print "ListView.dropped"


class ListViewItem(QListViewItem) :

   def __init__(self, parent, text) :
      QListViewItem.__init__(self, parent, text)

   def dragEntered(self) :
      print "ListViewItem.dragEntered"

   def acceptDrop(self, mimeSource) :
      print "ListViewItem.acceptDrop"

      return 1

   def dropped(self, dropEvent) :
      print "ListViewItem.dropped"

      qStr = QString()
      decode = QTextDrag.decode(dropEvent, qStr)
      if (not decode) :
         return

      itemName = str(self.text(0))
      droppedItemName = str(qStr)
      self.listView().emit(PYSIGNAL("dropOnItem"), (itemName, droppedItemName))


class dnd (QWidget) :
   def __init__(self, parent = None, name = None) :
      QWidget.__init__(self, parent, name)

      self.listview1 = ListView(self)
      self.listview2 = ListView(self)

      self.listview1.addColumn("col1")
      self.listview2.addColumn("col2")

      self.listview1.setGeometry(QRect(45,31,221,250))
      self.listview2.setGeometry(QRect(316,32,231,240))

      self.listview1.show()
      self.listview2.show()
      self.listview2.setAcceptDrops(1)

      qlvi1 = QListViewItem(self.listview1, "Item11")
      qlvi1.setDragEnabled(1)

      qlvi2 = ListViewItem(self.listview2, "Item21")
      qlvi2.setDropEnabled(1)



if __name__ == "__main__" :

   import sys
   qa = QApplication(sys.argv)

   d = dnd()
   d.show()

   qa.setMainWidget(d)
   qa.exec_loop()


More information about the PyQt mailing list