[PyQt] Dropping on ListView

Boudewijn Rempt boud at rempt.xs4all.nl
Wed Aug 29 21:42:51 CEST 2001


Toralf Wittner <wittner at fossilverlag.de> wrote:
> Hello world,

> consider the following code fragment that is supposed to print the 
> pointer to the QListViewItem a text is dropped on. Unfortunatly it 
> doesn't work. I used the function 'QListViewItem* itemAt(const QPoint & 
> screenPos)' that should return the QListViewItem, but it just doesn't do it.

> Maybe someone is able to tell me what's wrong here and how I could master 
> this problem. Thanks for any comments!

Well, first of all, it's really the QListView that should accept the
drops, not the main widget - you can just as easily use a QListView as
a main widget, so I moved the dropEvent code to the QListView.

Then, the header takes up some space, too. You need to subtract the
height of the header from the drop location. Below is some code that
works (only because it rather messes about to generate drop events,
it'll segfault nine times out of ten - never mind about that).


#!/usr/bin/env python
 
from sys import argv
from qt import *
 
 
class DropDemo(QListView):
        def __init__(self,parent=None,name=None):
                QListView.__init__(self,parent,name)
                self.setAcceptDrops(1)
                self.setGeometry(10,10,100,60)
                self.addColumn("Column 1")
                self.i = QListViewItem(self, "Hello")
                self.ii = QListViewItem(self, "Hello 2")
                print "header", self.header().height()
                r = self.itemRect(self.i)
                print self.i, r.x(), r.y(), r.x() + r.width(), r.y() + r.height()
                r = self.itemRect(self.ii)
                print self.ii, r.x(), r.y(), r.x() + r.width(), r.y() + r.height()
                
        def dragEnterEvent(self, event):
                event.accept(QTextDrag.canDecode(event))
  
        def dropEvent(self, event):
                text = QString()
                print event.pos().x(), event.pos().y()- self.header().height()
                pos = QPoint(event.pos().x(), event.pos().y() - self.header().height())
                item = self.itemAt(pos)
                print item

        def mouseMoveEvent(self, event):
                self.startDrag()

        def startDrag(self):
                d = QTextDrag("blabla",self) # keep a reference to d
                d.dragCopy()
 
if __name__ == '__main__':
        a = QApplication(argv)
        w = DropDemo()
        w.setCaption("Drag and Drop Test")
        w.resize(120,80)
        a.setMainWidget(w)
        w.show()
        a.exec_loop()

-- 

Boudewijn Rempt  | http://www.valdyas.org 



More information about the Python-list mailing list