在一个程序中,我想接受将文件拖放到QTextExit
中以编辑它的某些部分。
拖放工作正常,但是在QTextEdit
中删除文件后,QTextEdit
的光标会冻结(停止闪烁,无法再移动)。< / p>
这是一个最小的例子:
拖放后: (d&amp; d工作正常,但光标冻结)
我可以编辑textEdit中的内容,但是在textEdit失去焦点后光标仍然可见。
代码:
# -*- coding: utf-8 -*-
import sys
from PyQt5 import Qt
if __name__ == '__main__':
Application = Qt.QApplication(sys.argv)
from Drag_drop_window import Ui_Form # import QtDesigner file
class override_textEdit(Qt.QTextEdit): # override drop event for QTextEdit
drop_accepted_signal = Qt.pyqtSignal(str) # the signal sends the file name to other widget
def __init__(self):
super(override_textEdit,self).__init__()
self.setText("123")
self.setAcceptDrops(True)
def dropEvent(self, event):
if len(event.mimeData().urls())==1: # accept event when only one file was dropped
event.accept()
self.drop_accepted_signal.emit(event.mimeData().urls()[0].toLocalFile())
else:
Qt.QMessageBox.critical(self,"Accept Single File Only","Accept Single File Only",Qt.QMessageBox.Abort)
event.ignore()
class myWidget(Qt.QWidget):
def __init__(self):
super(myWidget, self).__init__()
self.main = Ui_Form()
self.main.setupUi(self)
self.main.textEdit = override_textEdit()
self.main.verticalLayout.addWidget(self.main.textEdit)
self.main.textEdit.drop_accepted_signal.connect(self.slot)
self.show()
def slot(self,filename):
self.main.lineEdit.setText(filename) # display file name in lineEdit
if __name__ == '__main__':
my_Qt_Program = myWidget()
my_Qt_Program.show()
sys.exit(Application.exec_())
由QtDesigner生成的Drag_drop_window.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Drag_drop_window.ui'
#
# Created: Sun Apr 5 17:53:03 2015
# by: PyQt5 UI code generator 5.3.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
答案 0 :(得分:3)
我发现在dropEvent
的末尾添加以下四行可以解决问题:
mimeData = QtCore.QMimeData()
mimeData.setText("")
dummyEvent = QtGui.QDropEvent(event.posF(), event.possibleActions(),
mimeData, event.mouseButtons(), event.keyboardModifiers())
super(override_textEdit, self).dropEvent(dummyEvent)
您已覆盖dropEvent
控件的QTextEdit
方法,但您并未调用重写的超类方法。我想在重写的方法中有一些清理代码需要运行才能解决你对光标的问题。但是,只需使用drop事件调用超类方法,即
super(override_textEdit, self).dropEvent(event)
没有做你想做的事。这会将删除的文件的URL输入到文本编辑控件中。
我没有发现在调用超类accept()
之前调用ignore()
,setDropAction(Qt.IgnoreAction)
或dropEvent
的任何组合都有效。我怀疑超类方法决定是否接受或忽略事件,这可能会覆盖你的子类将要做的事情。
所以相反,我创造了一个假的&#39;拖放事件,与收到的事件相同,只是文本数据为空,并将此假事件传递给超类。当然,欢迎超类在某个地方插入这个空文本,但是如果有的话,它不会产生任何影响。