Code: Select all
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)
Code: Select all
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)
Code: Select all
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()