Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag and drop multiple files into UI to pull file info
Forum Updated to NodeBB v4.3 + New Features

Drag and drop multiple files into UI to pull file info

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Muuk
    wrote on last edited by
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • M Muuk

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Might be a silly question but did you call setAcceptDrops on your button ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @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.

          M Offline
          M Offline
          Muuk
          wrote on last edited by Muuk
          #4

          @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')
          
          
          JonBJ 1 Reply Last reply
          0
          • M Muuk

            @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')
            
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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 subclassed QMainWindow you can write them there, but that will receive events for anywhere on the main window, not just on the pushbuttons. And hence you print("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....

            M 1 Reply Last reply
            1
            • JonBJ JonB

              @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 subclassed QMainWindow you can write them there, but that will receive events for anywhere on the main window, not just on the pushbuttons. And hence you print("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....

              M Offline
              M Offline
              Muuk
              wrote on last edited by
              #6

              @JonB I can't figure out how to subclass this thing.

              JonBJ 1 Reply Last reply
              0
              • M Muuk

                @JonB I can't figure out how to subclass this thing.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Muuk
                Sorry, but subclassing is a standard thing in Python; if you don't know about it you need to read up. The code you show is a perfect example of subclassing; you would need to apply that technique to subclassing a QPushButton.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved