How to drag and drop files into a PyQt4 interface?

Viewed 277

I currently have a textEdit widget inside a qt gui interface. In Linux I am able to drag and drop files from the system into it and it will fill the widget with "file:///path/to/file". The file:// part is slightly annoying but I can sift that out. However, when I try to drag and drop a file from Windows, the widget rejects it altogether. How do I get it accept Windows files as well?

main python file with relevant code:

import os #to get current working directory
import sys
from ui import Ui_Form #Ui_Form contains the code for the display. Generated by QT Designer
from PyQt4 import QtCore, QtGui #code for Qt

class UI(Ui_Form):
    def setupUi(self, Form):
        super().setupUi(Form)
        self.browseButton.clicked.connect(self.browse)

    def browse(self):
        self.lineEdit.setText(QtGui.QFileDialog.getOpenFileName(self.lineEdit, "Open File", os.getcwd(), "CSV (*.csv)") )       

app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = UI()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())

And the following code I generated with QTDesigner:

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

# Form implementation generated from reading ui file 'interface.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(656, 271)
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(180, 10, 301, 21))
        self.label.setObjectName(_fromUtf8("label"))
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(40, 40, 431, 29))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.browseButton = QtGui.QPushButton(Form)
        self.browseButton.setGeometry(QtCore.QRect(490, 40, 131, 29))
        self.browseButton.setObjectName(_fromUtf8("browseButton"))

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

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Test", None))
        self.label.setText(_translate("Form", "Testing", None))
        self.lineEdit.setPlaceholderText(_translate("Form", "Path to File", None))
        self.browseButton.setText(_translate("Form", "Browse", None))

I edited some of the non-essential parts out.

0 Answers
Related