[Python] Python function eventFilter does not receive drag or drop after installEventFilter

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am using PySide6. I have an application with the user interfaces defined in files apart from the code of each window and a file with the main window that makes use of them. So far a fairly standard construction. I want a text field to be able to either drag text or a file, in which case it should read it and load its contents. The problem arises when I set the field to accept drops and install a filter for the events, but the events that are received are neither drag nor drop. I have prepared a minimal example with a user interface file, a file with the event control code and a main window file. Once running, if you drag a text file into the text field, the only thing that loads is the Url of the file. The print() shows that the filter is entered, but neither drag nor drop is received. What is going on here?

    File with the user interface up_ui.py

    from PySide6.QtWidgets import (QPlainTextEdit, QVBoxLayout)

    class Ui_Up(object):
    def setupUi(self, Up):
    if not Up.objectName():
    Up.setObjectName(u"Up")
    Up.resize(420, 600)
    Up.setModal(True)

    self.txtInput = QPlainTextEdit(Up)
    self.txtInput.setEnabled(True)

    self.VL = QVBoxLayout(Up)
    self.VL.addWidget(self.txtInput)


    File with the User interface code up.py

    from PySide6.QtCore import QEvent
    from PySide6.QtWidgets import QDialog

    from up_ui import Ui_Up

    class Up(QDialog, Ui_Up):
    def __init__(self):
    super().__init__()
    self.setupUi(self)

    # Accept drop events
    self.txtInput.setAcceptDrops(True)
    self.txtInput.installEventFilter(self)

    def eventFilter(self, obj, event):
    print(f"Event: {event.type()}")
    print(f"QEvent.DragEnter = {QEvent.DragEnter}")
    print(f"QEvent.Drop = {QEvent.Drop}")
    if id(obj) == id(self.txtInput):
    print("txtInput")
    if event.type() == QEvent.DragEnter:
    print("Drag")
    if (event.mimeData().hasText() or
    event.mimeData().hasUrls()):
    event.acceptProposedAction()
    return True
    elif event.type() == QEvent.Drop:
    print("Drop")
    if event.mimeData().hasUrls():
    # URL list
    for url in event.mimeData().urls():
    print(url)
    # Link or file
    link = url.toString()
    file_path = url.toLocalFile()
    print(file_path)
    if file_path.lower().endswith(('.txt', '.text')):
    # Text file
    try:
    with open(file_path, 'r', encoding='utf-8') as file:
    content = file.read()
    # Load content
    self.txtInput.setPlainText(content)
    except Exception as e:
    self.txtInput.setPlainText(f"Error:\n{e}")
    elif event.mimeData().hasText():
    # Text
    text = event.mimeData().text()
    # Load text
    self.txtInput.setPlainText(text)
    event.acceptProposedAction()
    return True
    return super().eventFilter(obj, event)


    File with the main window main.py

    from PySide6.QtWidgets import (QApplication, QMainWindow)

    from up import Up

    class Main(QMainWindow):
    def __init__(self):
    super().__init__()
    #self.setupUi(self)

    self.update = Up()
    self.update.show()


    if __name__ == "__main__":
    app = QApplication([])
    wMain = Main()
    wMain.show()
    app.exec()

    Continue reading...

Compartilhe esta Página