所用编程语言:python 2.7.10
#coding:utf-8
from __future__ import unicode_literals,division
import sys
from PyQt4 import QtGui, QtCore
class TextEdit(QtGui.QTextEdit):
def __init__(self,*args,**kw):
super(TextEdit, self).__init__(*args,**kw)
self.setAcceptDrops(True)
def canInsertFromMimeData(self,mimeData):
if mimeData.hasUrls():
return True
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
super(TextEdit, self).dragEnterEvent(event)
def dragMoveEvent(self, event):
super(TextEdit, self).dragMoveEvent(event)
def dropEvent(self, event):
if event.mimeData().hasUrls():
for url in event.mimeData().urls():
print unicode(url.toLocalFile())
event.acceptProposedAction()
else:
super(TextEdit,self).dropEvent(event)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow,self).__init__()
self.setGeometry(100,100,300,400)
self.setWindowTitle("QTextEdit文件拖放示例")
self.te = TextEdit(self)
self.te.setGeometry(QtCore.QRect(90, 90, 61, 51))
self.te.setText("UVE3ODI4Nzg0NDg=")
self.setCentralWidget(self.te)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())