微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将文件拖放到PyQt4界面?

我目前在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,disambig,_encoding) except AttributeError: def _translate(context,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,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))

我编辑了一些非必要的部分。

处理拖动(从DragOver事件)时是否可以更改鼠标光标?

在运行时拖动窗体上的控件

如果应用程序位于特定的目录中,则拖放不起作用

使标题图标变成资源pipe理器窗口的拖动源

简单的GUI Windows拖放

从Windows资源pipe理器拖放到我的应用程序的文本框中

各种剪贴板/拖放格式是什么意思?

如何拖拽大量的文件而不消耗系统资源?

如何获得新的Windows拖放游标

如何处理batch file中%*不一致的文件名格式

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐