我目前在qt gui界面中有一个textEdit小部件。在Linux中,我能够将文件从系统拖放到它中,它将使用" file:/// path / to / file"填充小部件。文件://部分有点烦人但我可以筛选出来。但是,当我尝试从Windows拖放文件时,窗口小部件完全拒绝它。我如何让它接受Windows文件?
主python文件及相关代码:
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_())
以下我用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))
我编辑了一些非必要的部分。