PySide6文本框拖拽事件的问题
本帖最后由 小朋友呢 于 2024-4-12 12:09 编辑1.如果所示,在向TextEdit拖入文件并读取内容后,它的光标,就不闪了,一直定在这
2.下面是我的处理拖拽事件的代码,试过网上很多的激活跟设置焦点的方法,但它的光标就是定住的状态,求助一下各位大佬如何解决
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
event.acceptProposedAction()
def dropEvent(self, event):
for url in event.mimeData().urls():
file_path = url.toLocalFile()
content = get_file_content(file_path)
self.ui.plainTextEdit_cert.setPlainText(content)
self.ui.plainTextEdit_cert.setFocus()
event.acceptProposedAction() 在你重写的dropEvent事件内先执行父类的dropEvent,比如super().dropEvent(event),很久没写Python了忘得差别不多了不知道有没有写对,如果不是dropEvent的问题再看看dragEnterEvent hrpzcf 发表于 2024-4-11 15:27
在你重写的dropEvent事件内先执行父类的dropEvent,比如super().dropEvent(event),很久没写Python了忘得差 ...
大佬,我试了一下用super,因为我是自定义类,不是控件类,发现还是不行,后来我就保存了原来的dropEvent事件,调用自己的dropEvent事件的时候,就调用一下原来的dropEvent就可以了
self.save_dropEvent = self.ui.plainTextEdit_cert.dropEvent
self.ui.plainTextEdit_cert.dropEvent = self.dropEvent 本帖最后由 hrpzcf 于 2024-4-11 17:45 编辑
小朋友呢 发表于 2024-4-11 16:29
大佬,我试了一下用super,因为我是自定义类,不是控件类,发现还是不行,后来我就保存了原来的dropEvent ...
我没仔细看,我还以为你的类是继承自QPlainTextEdit然后重写dropEvent呢
class PlainTextEdit(QPlainTextEdit):
def __init__(self, accept: Accept = None, ext_filter: set = None):
super().__init__()
self.setLineWrapMode(QPlainTextEdit.NoWrap)
if accept is None:
self.__accept = Accept.File
else:
self.__accept = accept
if ext_filter is None:
self.__filter = set()
else:
assert isinstance(ext_filter, set)
self.__filter = ext_filter
self.__drag_temp = list()
@property
def local_paths(self):
file_dir_paths = self.toPlainText().split("\n")
if self.__accept == Accept.Dir:
return
if self.__accept == Accept.File:
return
return list()
def __stash_from_urls(self, urls: List):
self.__drag_temp.clear()
for file_or_dir in (path.toLocalFile() for path in urls):
file_or_dir = os.path.realpath(file_or_dir)
if os.path.isfile(file_or_dir):
self.__drag_temp.append(file_or_dir)
continue
self.__drag_temp.append(file_or_dir)
for root, _, files in os.walk(file_or_dir):
self.__drag_temp.extend(
os.path.join(root, filename) for filename in files
)
def dragEnterEvent(self, event: QDragEnterEvent):
self.__drag_temp.clear()
if event.mimeData().hasUrls():
if self.__accept == Accept.File:
self.__stash_from_urls(event.mimeData().urls())
if not self.__filter or set(
os.path.splitext(fp)
for fp in self.__drag_temp
if os.path.isfile(fp)
).issubset(self.__filter):
event.accept()
else:
event.ignore()
elif self.__accept == Accept.Dir:
event.accept()
else:
event.ignore()
if not self.toPlainText().endswith("\n"):
self.appendPlainText("")
else:
event.ignore()
def dropEvent(self, event: QDropEvent):
cur_text = self.toPlainText()
super().dropEvent(event)
if not self.__drag_temp:
self.__stash_from_urls(event.mimeData().urls())
if self.__accept == Accept.File:
self.setPlainText(
cur_text
+ "\n".join(p for p in self.__drag_temp if os.path.isfile(p))
)
elif self.__accept == Accept.Dir:
self.setPlainText(
cur_text
+ "\n".join(p for p in self.__drag_temp if os.path.isdir(p))
)
else:
self.setPlainText("")
self.verticalScrollBar().setValue(
self.verticalScrollBar().maximumHeight()
)
学习学习
页:
[1]