PyQt Drag and Drop - Nothing happens
Posted
by Umang
on Stack Overflow
See other posts from Stack Overflow
or by Umang
Published on 2010-05-20T07:10:05Z
Indexed on
2010/05/20
14:30 UTC
Read the original article
Hit count: 351
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!
© Stack Overflow or respective owner