Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Drag and Drop adding the QUrl as well as the file path in QLineEdit
Forum Updated to NodeBB v4.3 + New Features

Drag and Drop adding the QUrl as well as the file path in QLineEdit

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.5k Views
  • 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.
  • A Offline
    A Offline
    AddyMills
    wrote on last edited by
    #1

    Hello all,

    I'm just getting into the world of Qt and I've run into an odd situation that I can't seem to solve. I've created a GUI with a QLineEdit widget and enabled drag and drop in order to grab the file path, and while it works to drop in a file, there is extra data that gets added. Say I drag a file C:/real.pak.xen into my program. The QLineEdit field then shows C:/real.pak.xenfile:///C:/real.pak.xen.

    The "file:///C:/real.pak.xen" is what's stored in the QUrl of the file and is unwanted. It shows up where I released my mouse button. If I were to drag and drop this same file closer to the left of the input box, it displays "C:/reafile:///C:/real.pak.xenl.pak.xen" instead.

    This is my code snippet to initialize the field:

    self.pak_input_field = QLineEdit(self)
    self.pak_input_field.setPlaceholderText('Drag and drop PAK file or click to browse...')
    self.pak_input_field.setAcceptDrops(True)
    self.pak_input_field.installEventFilter(self)
    layout.addWidget(self.pak_input_field)
    

    Because I want the user to be able to drag and drop files as well as browse for them, this is my event filter:

        def eventFilter(self, obj, event):
            if obj == self.pak_input_field and event.type() == QEvent.DragEnter:
                if event.mimeData().hasUrls():
                    event.acceptProposedAction()
                else:
                    event.ignore()
            elif obj == self.pak_input_field and event.type() == QEvent.Drop:
                file_path = event.mimeData().urls()[0].toLocalFile()
                self.set_input_filepath(file_path)
                event.acceptProposedAction()
            elif obj == self.pak_input_field and event.type() == QEvent.MouseButtonPress:
                options = QFileDialog.Options()
                filename, _ = QFileDialog.getOpenFileName(self, "Open PAK File", "",
                                                          "PAK Files (*.pak *.pak.xen *.pak.ps3)", options=options)
                if filename:
                    self.set_input_filepath(filename)
            return super().eventFilter(obj, event)
    

    I figure the QEvent.Drop portion has a fault somewhere, but I can't figure out what. Clearing the field before setting the text in the field doesn't work either.

    Does anyone have any ideas what could be happening?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      friedemannkleint
      wrote on last edited by
      #2

      Dnd may have additional info depending on the source. The drop site example can be used to display it https://doc.qt.io/qtforpython-6/examples/example_widgets_draganddrop_dropsite.html .

      A 1 Reply Last reply
      1
      • F friedemannkleint

        Dnd may have additional info depending on the source. The drop site example can be used to display it https://doc.qt.io/qtforpython-6/examples/example_widgets_draganddrop_dropsite.html .

        A Offline
        A Offline
        AddyMills
        wrote on last edited by
        #3

        @friedemannkleint Yes! Thank you!

        That allowed me to see that it was the "text/uri-list" data it was inserting. I ended up re-defining the Drop function for the QLineEdit class and that seemed to have done the trick!

        class FileDropLineEdit(QLineEdit):
            def __init__(self, parent=None):
                super().__init__(parent)
        
            def dropEvent(self, event):
                if event.mimeData().hasUrls():
                    file_path = event.mimeData().urls()[0].toLocalFile()
                    file_path = file_path.replace("/", "\\")
                    self.setText(file_path)
                    event.acceptProposedAction()
                else:
                    event.ignore()
        

        Since it's now its own class, I also removed the reference to the Drop event in my event filter and referenced it when creating the initial window.

        1 Reply Last reply
        0
        • A AddyMills has marked this topic as solved on

        • Login

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