[PyQt] Drag and drop files

non7top at gmail.com non7top at gmail.com
Wed Jan 30 12:32:12 GMT 2008


В сообщении от Wednesday 30 January 2008 13:09:00 Tina I написал(а):

> I'm stuck! Simply put I want to drop files into a QListView. I have been
> reading TFM about QEvent but I don't understand what I'm reading (I'm
> fairly new to programming) so I was hoping someone with too much time on
> their hands would kickstart me a little. I just need some sample code to
> point me in the right direction. I'm attaching a simple GUI that only
> contain the ListView and a barebones .py to display it. Basically I just
> want know how to catch the drop, atleast for now.

Here's some code snippets from my app (self.ui.list_links is a QListWidget, 
but i think that it would be same with QListView). Note that you should 
reimplement both dragEnterEvent and dragMoveEvent (the letter is not required 
for QLineEdit)

class MyForm(QtGui.QMainWindow):
        def __init__(self, parent=None):
                QtGui.QWidget.__init__(self, parent)
                self.ui.list_links.__class__.dragEnterEvent = 
self.DragEnterEvent
                self.ui.list_links.__class__.dragMoveEvent = 
self.DragEnterEvent
                self.ui.list_links.__class__.dropEvent = self.drop
		self.ui.list_links.setAcceptDrops(1)
	

        def DragEnterEvent(self, event):
                if event.mimeData().hasFormat("text/plain"):
                        event.accept()
                else:
                        event.ignore()

        def drop(self, event):
                link=event.mimeData().text()
                print link


-- 
С уважением Владимир В. Бережной



More information about the PyQt mailing list