0

I am learning the drag and drop part of pyqt5. However, most files can be dragged and dropped, but only Outlook's mail is impossible. (I need the location of the file) I am looking for help because the Outlook mail is stored in a .pst format file and does not seem to be dragged and dropped immediately. There are two options I thought of. First, drag and drop directly from Outlook to register mail path in pyqt. Second, if you drag it from Outlook to pyqt, download the mail to a specific location and register the path in pyqt.

Is there a better way? Or can you give me advice on the two methods I suggested?

import sys

from PyQt5.QtCore import Qt, QFile, QIODevice, QDataStream
from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidget, QListWidgetItem, QPushButton


class ListBoxWidget(QListWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setAcceptDrops(True)
        self.resize(600, 600)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(Qt.CopyAction)
            event.accept()
        elif event.mimeData().hasFormat("FileContents"):
            """
            1. How can I drag the OUTLOOK mail right away?
            2. How can I download OUTLOOK mail when it enters the PYQT area?
            """
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(Qt.CopyAction)
            event.accept()

            links = []
            for url in event.mimeData().urls():
                # https://doc.qt.io/qt-5/qurl.html
                if url.isLocalFile():
                    links.append(str(url.toLocalFile()))
                else:
                    links.append(str(url.toString()))
            self.addItems(links)
        else:
            event.ignore()


class AppDemo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(1200, 600)

        self.listbox_view = ListBoxWidget(self)

        self.btn = QPushButton('Get Value', self)
        self.btn.setGeometry(850, 400, 200, 50)
        self.btn.clicked.connect(lambda: print(self.getSelectedItem()))

    def getSelectedItem(self):
        item = QListWidgetItem(self.listbox_view.currentItem())
        return item.text()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    demo = AppDemo()
    demo.show()

    sys.exit(app.exec_())

It's not easy to find data on PYQT5. Please help me.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
조현성
  • 13
  • 3
  • What is the problem? Does your code drop something else? What are the available formats of the mime data? – musicamante Feb 07 '22 at 13:41
  • 1
    See https://stackoverflow.com/questions/17184369/upload-fails-when-user-drags-and-drops-attachment-from-email-client/17197224#17197224 – Dmitry Streblechenko Feb 07 '22 at 16:19
  • @musicamante If I drag the Outlook mail in the pyqy area, the mail will be downloaded at a specific location and I want to print the path to pyqt. – 조현성 Feb 08 '22 at 05:50
  • @DmitryStreblechenko That's right. It's the same as I thought. But the question is how to actually write this code. – 조현성 Feb 08 '22 at 05:54

0 Answers0