Drag and Drop adding the QUrl as well as the file path in QLineEdit
-
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?
-
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 .
-
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 .
@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.
-