Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QApplication.clipboard() copy-paste issue

QApplication.clipboard() copy-paste issue

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    midnightdim
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        midnightdim
        wrote on last edited by midnightdim
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          From the looks of it, there's an issue with the object lifetime handling. You should open a ticket on the bug report system with your example.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Michae9
            wrote on last edited by
            #5

            Hello @midnightdim ,
            did you find any soulution? I am facing the 2nd issue
            (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')])
            in an application that i am running on (mac, ios and android).
            The problem occurs only in iOS build.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved