Drag and drop multiple files into UI to pull file info
-
I am trying to figure out a way to drag and drop multiple files onto a pushbutton. Not drag the button around itself, but drag and drop files onto the button.
Basically my button has two functions. Main function, you click it, and it will pull a file, from a preset folder to input them into a tracker. Second (the one I cannot get to work for the life of me) you drag the files from anywhere, onto the button which then initiates that upload to the tracker.
I can get the button to pull files, but I cannot get the button to recognize a file was dropped on it. I have been using dragEnterEvent, dragMoveEvent, and dropEvent. None of which seem to work.
can this not be done with buttons? Do I need to do something else?
-
@Muuk said in Drag and drop multiple files into UI to pull file info:
I have been using dragEnterEvent, dragMoveEvent, and dropEvent.
Please show the code. It should work if you subclass QPushButton and override the event handlers.
-
Hi and welcome to devnet,
Might be a silly question but did you call setAcceptDrops on your button ?
-
@jsulm and @SGaist
Not sure if it matters, but I have been using the QT5 Designer. Most guides I am finding speak about setting up custom buttons, I have just been using the designer, and I don't have buttons subclassed the same way they do it from scratch.Again I am trying to set it up so that when a file is dragged and dropped onto one of those push buttons "pbOpen" etc, it will pull that files location.
All it currently does is print K the moment the file goes anywhere on the GUI. I am just dumb and cannot figure out how to assign it to just the buttons, instead of the entire main window.
class Ui(QtWidgets.QMainWindow): def __init__(self): super(Ui, self).__init__() uic.loadUi(fr"{temp}\modules\gui\ui\PGUI.ui", self) self.show() self.setWindowTitle(title) self.loadWidgets() self.setAcceptDrops(True) def dragEnterEvent(self, event): print("k") print(event.mimeData().urls()) def dragMoveEvent(self, event): print("j") event.setDropAction(Qt.CopyAction) def dropEvent(self, event): print("l") event.setDropAction(Qt.CopyAction) #add button functions below def loadWidgets(self): self.pbOpen.clicked.connect(self.openWO) self.pbUpdate.clicked.connect(self.updateWO) self.pbClose.clicked.connect(self.closeWO) def openWO(self): x = threading.Thread(target=PyPAS.openWO) x.start() def updateWO(self): print ('update button') self.playSound('errorSound.mp3') def closeWO(self): print('close button') self.playSound('errorSound.mp3')
-
@Muuk said in Drag and drop multiple files into UI to pull file info:
Not sure if it matters, but I have been using the QT5 Designer. Most guides I am finding speak about setting up custom buttons, I have just been using the designer, and I don't have buttons subclassed the same way they do it from scratch.
Because the
drag
/drop...
methods are events you have to subclass the receiving widget to write your code for them. You have subclassedQMainWindow
you can write them there, but that will receive events for anywhere on the main window, not just on the pushbuttons. And hence youprint("k")
all over the main window. To get it for only the pushbuttons you will have to do one of two things:-
Stick with only subclassing
QMainWindow
, but look at and deal with mouse coordinates yourself, recognising when they are over your pushbuttons. -
Subclass the pushbuttons and override the event methods in them only. This feels better, but you will need to subclass your
QPushButtons
. If using Designer that means using Promote, which you can look up in its Help. And I am not sure how easy that it is to do from Python instead of C++.
So not sure which approach is simplest for you....
-