Skip to content

Instantly share code, notes, and snippets.

@jeakwon
Created June 27, 2020 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeakwon/6b1812bcb4539264c6cc4d4cb7861fab to your computer and use it in GitHub Desktop.
Save jeakwon/6b1812bcb4539264c6cc4d4cb7861fab to your computer and use it in GitHub Desktop.
drag and drop multi document interface sample code
import os, sys
from PySide2.QtWidgets import QTextEdit, QApplication, QMainWindow, QMdiArea, QMdiSubWindow, QMessageBox
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setCentralWidget(MdiArea())
class MdiArea(QMdiArea):
def __init__(self, parent=None):
super(MdiArea, self).__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
event.acceptProposedAction()
def dropEvent(self, event):
txts = []
for url in event.mimeData().urls():
if (url.scheme()=='file') & (url.path().endswith('.txt')):
sub = QMdiSubWindow()
sub.setWidget(QTextEdit())
sub.setWindowTitle(os.path.basename(url.path()))
self.addSubWindow(sub)
sub.show()
else:
dialog = QMessageBox()
dialog.setWindowTitle("Error: Invalid File")
dialog.setText("Only .txt files are accepted")
dialog.setIcon(QMessageBox.Warning)
dialog.exec_()
return
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment