0

What I want: Drag a file into label area, and get the file directory.

The problem is: I have 2 Class, 1 is for drag&drop function, the other one is for creating ui window. I don't know how to apply the drag&drop function to the label inside the ui.

from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QLabel

# class that add drag&drop function to QLabel
class Label_draghere(QLabel):
    def __init__(self, parent):
        super().__init__(parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()

    def dropEvent(self, event):
        # print file directory
        lines = []
        for url in event.mimeData().urls():
            lines.append('dir: %r' % url.toLocalFile())
        print(lines)

# class that create ui window
class Win_excelCheck:
    def __init__(self):
        # load ui (there's only 1 Label named label_drag1 in the ui, nothing else)
        self.ui = uic.loadUi('ui/label_win.ui')
        self.ui.setAcceptDrops(True)

        # This line is wrong, what I'm tring to do is apply Label_draghere Class to label_drag1 widget
        self.ui.label_drag1 = Label_draghere(self.ui)


if __name__ == '__main__':
    app = QApplication([])
    mainWin = Win_excelCheck()
    mainWin.ui.show()
    app.exec_()

Code Result: This code has no error message showed up, but when I drag file into the Label, there's a red prohibition sign next to my cursor and doesn't allow me to drop anything. img_drop_prohibited

My reference: I wrote this code by referring to this: https://www.jb51.net/article/226682.htm .But all the tutorials use addWidget to create ui instead of uic.loadUi, and I need to use uic way, so I don't know how to modify the code.

Please help, really appreciate ^_^

HT L.
  • 5
  • 5
  • Use the promotion, see the duplicate code – eyllanesc Nov 16 '21 at 02:26
  • What dnd is useless in this case, the real problem is like using a Qwidget (QLabel) in a .ui. In your case is a QLabel that implements the dnd and in my solution is a clickable qlabel so the solution is the same. – eyllanesc Nov 16 '21 at 02:34
  • I'm really new to python, I read you answer but still don't understand. Could you help me modify my code or be more specific? i'm not quite understand "promote". – HT L. Nov 16 '21 at 02:46
  • what is dnd? i'm really new to programming T.T – HT L. Nov 16 '21 at 02:47
  • oh you mean drag and drop. got it. – HT L. Nov 16 '21 at 02:48
  • I still can't figure it out ... – HT L. Nov 16 '21 at 03:26
  • I don't quite understand how to deal with "class", "parent", "self", "super" ... in my code – HT L. Nov 16 '21 at 03:27
  • Take your time, implement my example https://stackoverflow.com/a/45576053/6622587 and analyze it. Learning is not easy. – eyllanesc Nov 16 '21 at 03:27
  • If you don't understand those basics then read any basic python article. On the other hand, I recommend you change to : `def __init__(self, parent=None):` – eyllanesc Nov 16 '21 at 03:29
  • I tried. but I don't know where should I put `self.setAcceptDrops(True)` if I don't use `def __init__(self, parent). and i don't understand "generate the .ui file with pyuic" , and I didn't see you load .ui file in your code ... too much questions... – HT L. Nov 16 '21 at 03:32
  • Why don't you want to use that? Where in my answer do I point out that you shouldn't use `__init__` ? – eyllanesc Nov 16 '21 at 03:35

0 Answers0