realhuhu 发表于 2021-2-7 12:24:41

PyQt QLineEdit DropEvent无法拖入文件

本帖最后由 realhuhu 于 2021-2-7 12:27 编辑

从pycharm可以正常拖入文件获得路径,但是其他地方的文件就不行了,怎么破
代码:
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit


class MyQLine(QLineEdit):

    def __init__(self):
      super().__init__()
      self.setAcceptDrops(True)

    def dropEvent(self, e):
      path = e.mimeData().text()
      self.setText(path)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = MyQLine()
    myWin.show()
    sys.exit(app.exec_())

hrp 发表于 2021-2-7 12:44:26

本帖最后由 hrp 于 2021-2-7 12:46 编辑

试试多重写一个dragEnterEvent方法,看看这个
https://fishc.com.cn/forum.php?mod=viewthread&tid=187937

realhuhu 发表于 2021-2-7 18:51:15

hrp 发表于 2021-2-7 12:44
试试多重写一个dragEnterEvent方法,看看这个
https://fishc.com.cn/forum.php?mod=viewthread&tid=187937

不行啊

hrp 发表于 2021-2-7 22:28:07

realhuhu 发表于 2021-2-7 18:51
不行啊

import sys

from PyQt5.QtWidgets import QApplication, QLineEdit


class MyQLine(QLineEdit):
    def __init__(self):
      super().__init__()

    def dragEnterEvent(self, e):
      e.accept()

    def dropEvent(self, e):
      path = e.mimeData().text()
      self.setText(path)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = MyQLine()
    myWin.show()
    sys.exit(app.exec_())

realhuhu 发表于 2021-2-8 11:57:58

hrp 发表于 2021-2-7 22:28


还是只有pycharm里的文件才能拖进去{:10_266:}

hrp 发表于 2021-2-8 13:13:35

realhuhu 发表于 2021-2-8 11:57
还是只有pycharm里的文件才能拖进去

没办法了,毕竟你问题描述太模糊了。
我运行正常。

realhuhu 发表于 2021-2-8 18:23:24

hrp 发表于 2021-2-8 13:13
没办法了,毕竟你问题描述太模糊了。
我运行正常。

可能跟环境有关,头疼
页: [1]
查看完整版本: PyQt QLineEdit DropEvent无法拖入文件