I am trying to build an application that will read the content of some files provided by the user. I built a window with a label, lineEdit, and q push button for the user to select the file, but I'd want to implement some Drag&Drop
The issue is that I cannot make the drag & drop event behave correctly, I have overridden the dragMoveEvent, dragEnterEvent, dragLeaveEvent, and dropEvent but those functions are not called when dragging/dropping a file or text in the widget
My current implementation (100% of my code) :
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QLabel, QTextEdit, QPushButton, QSpacerItem, \
QSizePolicy, QHBoxLayout, QLineEdit
class FileUploadWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.acceptDrops()
self.init_ui()
def init_ui(self):
self.setLayout(QHBoxLayout())
self.layout().addWidget(QLabel("File Selection", ))
self.layout().addWidget(QLineEdit())
self.layout().addWidget(QPushButton("Browse", ))
self.layout().addItem(QSpacerItem(10, 0, QSizePolicy.Minimum, QSizePolicy.Expanding))
self.layout().addWidget(QPushButton("Send", ))
def dragMoveEvent(self, a0: QtGui.QDragMoveEvent) -> None:
print("Drag move")
return super().dragMoveEvent(a0)
def dragEnterEvent(self, a0: QtGui.QDragEnterEvent) -> None:
print("Drag enter")
return super().dragEnterEvent(a0)
def dragLeaveEvent(self, a0: QtGui.QDragLeaveEvent) -> None:
print("Drag leave")
return super().dragLeaveEvent(a0)
def dropEvent(self, a0: QtGui.QDropEvent) -> None:
print("Drop event")
return super().dropEvent(a0)
def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
print("Mouse pressed")
return super().mousePressEvent(a0)
# class FileUploadWindow(QWidget):
# def __init__(self, parent=None):
# super(FileUploadWindow, self).__init__(parent)
# self.init_ui()
# self.acceptDrops()
# self.setMouseTracking(True)
#
# def init_ui(self):
# # Create a layout
# self.setLayout(QVBoxLayout())
# self.layout().addWidget(FileUploadWidget())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = FileUploadWidget()
window.show()
sys.exit(app.exec_())
Am I missing something ? The mousePressEvent does get triggered when clicking anywhere in the window
I am running Python 3.9.12 (tags/v3.9.12:b28265d, Mar 23 2022, 23:52:46) [MSC v.1929 64 bit (AMD64)] in a venv