Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QApplication.clipboard() copy-paste issue



  • I'm trying to implement copy-paste functionality in my file manager based on Qt/PySide.
    I'm facing the following problem with the clipboard.
    Here's my code (part of eventFilter()):

    # COPY code
    if event.key() == Qt.Key_C and event.modifiers() == Qt.ControlModifier: # CTRL+C
        urls = ... # urls is the list of QUrl objects created from selected file paths
        mime_data = QMimeData()
        mime_data.setUrls(urls)
        QApplication.clipboard().setMimeData(mime_data)
        print(QApplication.clipboard().mimeData().urls())
    # PASTE code
    elif event.key() == Qt.Key_V and event.modifiers() == Qt.ControlModifier: # CTRL+V
        # print(QApplication.clipboard().mimeData().urls())
        clipboard = QApplication.clipboard()
        print(clipboard.mimeData().urls())
        print(clipboard.mimeData().text())
    
    1. The first problem is: when I try to print(QApplication.clipboard().mimeData().urls() inside my PASTE code it causes this:

    RuntimeError: Internal C++ object (PySide2.QtCore.QMimeData) already deleted.

    But the same code works fine in my COPY code.
    Why does it work in one case and raises an error in another?

    1. The second problem is: when I run this and copy a couple of files using CTRL+C, these files are printed out:

    [PySide2.QtCore.QUrl('d:/test1.txt'), PySide2.QtCore.QUrl('d:/test2.txt')]

    But my PASTE code returns this:

    []
    test2.txt

    I.e. mimeData() doesn't seem to have any urls(), but it does have the text of the second file name (i.e. last file that was selected in COPY).
    How can I pass the file urls via mimeData()?

    Thanks.


  • Lifetime Qt Champion

    Hi,

    Which version of PySide2 is it ?
    On which platform ?
    Can you provide a minimal runnable script ?



  • PySide2 on Windows.

    Here's the MRS (it's in PySide6, but it's the same on PySide2):

    from PySide6.QtCore import *
    from PySide6.QtGui import *
    from PySide6.QtWidgets import *
    
    if __name__ == '__main__':
        class Dialog(QDialog):
            def __init__(self, parent=None):
                super(Dialog, self).__init__(parent)
                self.initUI()
                self.model = QFileSystemModel()
                self.model.setRootPath(".")
                self.view.setModel(self.model)
                self.view.setRootIndex(self.model.index("."))
                self.view.setSelectionMode(QAbstractItemView.ExtendedSelection)
                self.view.setRootIsDecorated(False)
                self.view.installEventFilter(self)
    
            def initUI(self):
                self.view = QTreeView(self)
                self.view.setWindowTitle('Simple Tree Model')
                self.view.setGeometry(10, 10, 600, 400)
                self.setGeometry(300, 300, 920, 620)
    
            def eventFilter(self, source, event):
                if source == self.view:
                    if event.type() == QEvent.KeyPress:             
                        if event.key() == Qt.Key_C and event.modifiers() == Qt.ControlModifier:
                            indexes = self.view.selectionModel().selectedRows()
                            urls = [QUrl(self.model.filePath(index)) for index in indexes]
                            mime_data = QMimeData()
                            mime_data.setUrls(urls)
                            clipboard = QApplication.clipboard()
                            clipboard.setMimeData(mime_data)
                            print(QApplication.clipboard().mimeData().urls())
                        elif event.key() == Qt.Key_V and event.modifiers() == Qt.ControlModifier:
                            # print(QApplication.clipboard().mimeData().urls())
                            clipboard = QApplication.clipboard()
                            print(clipboard.mimeData().urls())
                            print(clipboard.mimeData().text())
                return super(Dialog, self).eventFilter(source, event)
    
        import sys
    
        app = QApplication(sys.argv)
        dialog = Dialog()
        dialog.show()
        sys.exit(app.exec_())
    

    If you uncomment print(QApplication.clipboard().mimeData().urls()) it shows the error I mentioned. If you keep it like that and press CTRL+C then CTRL+V on some file(s) it won't print the expected urls.


Log in to reply