Vote count:
0
In a program, I want to accept drag and drop of a file in a QTextExit to edit certain parts of it, and display the file name in another QLineEdit.
The drag and drop works fine, however, after the file was dropped in QTextEdit, the cursor in it freezed (stopped blinking, cannot move around, keep exist after the focus was at other widget.
Here is a minimal example:
After drag-and-drop: (d&d works fine, but cursor freezed)
I can edit the content in the textEdit, but the cursor stays there.
codes:
# -*- 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_())
Drag_drop_window.py gengerated by QtDesigner
# -*- 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"))
asked 1 min ago
`QTextEdit` cursor freezed after overrided its `dropEvent`
Aucun commentaire:
Enregistrer un commentaire