views:

142

answers:

1

Hi,

I'm trying to get drop a file onto a Window (I've tried the same thing with a QListWidget without success there too)

test.py:

#! /usr/bin/python
# Test
from PyQt4 import QtCore, QtGui
import sys
from qt_test import Ui_MainWindow
class MyForm(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setupUi(self)
        self.__class__.dragEnterEvent = self.DragEnterEvent
        self.__class__.dragMoveEvent = self.DragEnterEvent
        self.__class__.dropEvent = self.drop
        self.setAcceptDrops(True)
        print "Initialized"
        self.show()

    def DragEnterEvent(self, event):
        event.accept()

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

def main():
    app = QtGui.QApplication(sys.argv)
    mw = MyForm()
    sys.exit(app.exec_())


if __name__== "__main__":
    main()

And here's qt_test.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created: Thu May 20 12:23:19 2010
#      by: PyQt4 UI code generator 4.6
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        MainWindow.setAcceptDrops(True)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))

I've read this email and I've followed everything said there. I still don't get any output except "Initialized" and the drag doesn't seem to get accepted (both for files from a file manager and plain text dragged from a text editor). Do you know what I'm doing wrong?

Thanks!

+1  A: 

Yes. Well... sort of.

Dragging plain text from an editor worked fine for me, as for files...

When you drop a file onto your app, it's type is "text/uri-list". For this you will want to use the event.mimeData().urls() method to get a list of PyQt4.QtCore.QUrl objects.

You will need to handle different mime data formats differently. You can use the following methods of the mimeData() to find out what attributes it has:

hasColor()
hasFormat()
hasHtml()
hasImage()
hasText()
hasUrls()
tgray
Dragging text doesn't work at all for me. The "+" mouse pointer doesn't appear, no text is printed in the terminal. Have you made any changes to the files provided?
Umang
Thanks! I managed to fix the problem. python-qt4 was migrated to squeeze yesterday so that seemed to have fixed it for me. I've accepted your answer because I hadn't found anything about the .urls() method when looking up about this elsewhere, so you saved me some looking up.
Umang
I found the .urls() method by using `dir(event.mimeData())`. If you aren't using windows, it might explain the differences in behavior.
tgray