SoFunction
Updated on 2024-10-29

Python PyQt5 implementation of drag and drop and clipboard functionality in detail

drag

The data managed by the QMimeData object based on the QDrag class for MIME type-based drag-and-drop data transfer is associated with its corresponding MIME type.

The MimeData class function allows detection and use of the method's MIME type

judgment function Setting Functions Get function MIME type
hasText() text() setText() text/plain
hasHtml() html() setHtml() text/html
hasUrls() urls() setUrls() text/uri-list
hasImage() imageData() setImageData() image/*
hasColor() colorData() setColorData() application/x-color

Common Drag and Drop Events

event descriptive
DragEnterEvent Triggered when a drag-and-drop control operation is performed and the mouse pointer enters the control.
DragMoveEvent This event is triggered when a drag-and-drop operation is performed.
DragLeaveEvent Triggered when a drag-and-drop control operation is performed and the mouse pointer leaves the control.
DropEvent Fires when the drag-and-drop operation is released on the target control
import sys
from  import *
from  import *
from  import *
from  import Qt

class Combo(QComboBox):
    def __init__(self, title, parent):
        super(Combo, self).__init__(parent)
        (True)
    
    def dragEnterEvent(self, e):
        print(e)
        if ().hasText():
            ()
        else:
            ()
    
    def dropEvent(self, e):
        (().text())

class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        ()
    
    def initUI(self):
        lo = QFormLayout()
        (QLabel("Please drag and drop the text on the left into the drop-down menu on the right."))
        edit = QLineEdit()
        (True)
        com = Combo("Button", self)
        (edit, com)
        (lo)
        ("Simple Drag and Drop Example")

if __name__ == "__main__":
    app = QApplication()
    win = Example()
    ()
    (app.exec_())

clipboard (computing)

The QClipboard class provides access to the system clipboard to copy and paste data between applications. It operates similarly to the QDrag class and uses similar data types.

The QApplication class has a static method clipboard() that returns a reference to the clipboard object. Any type of MimeData can be copied or pasted from the clipboard.

QClipboard common methods

methodologies descriptive
clear() Clearing the contents of the clipboard
setImage() Copying QImage objects to the clipboard
setMimeData() Setting MIME data to the clipboard
setPixmap() Copying Pixmap Objects from the Clipboard
setText() Copying text from the clipboard
text() Retrieving text from the clipboard

Common signals in the QClipboard class

code hidden meaning
dataChanged Triggered when the contents of the clipboard change
import os
import sys
from  import QMimeData
from  import (QApplication, QDialog, QGridLayout, QLabel, QPushButton)
from  import QPixmap

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        textCopyButton = QPushButton("&Copy Text")
        textPasteButton = QPushButton("Paste &Text")
        htmlCopyButton = QPushButton("C&opy HTML")
        htmlPasteButton = QPushButton("Paste &HTML")
        imageCopyButton = QPushButton("Co&py Image")
        imagePasteButton = QPushButton("Paste &Image")

         = QLabel("Original text")
         = QLabel()
        (QPixmap(((__file__), "images/")))

        layout = QGridLayout()
        (textCopyButton, 0, 0)
        (imageCopyButton, 0, 1)
        (htmlCopyButton, 0, 2)

        (textPasteButton, 1, 0)
        (imagePasteButton, 1, 1)
        (htmlPasteButton, 1, 2)

        (, 2, 0, 1, 2)
        (, 2, 2)
        (layout)

        ()
        ()
        ()
        ()
        ()
        ()

    def copyText(self):
        print(((__file__)))
        clipboard = ()
        ("I've been clipped")

    def pasteText(self):
        clipboard = ()
        (())
    
    def copyImage(self):
        clipboard = ()
        (QPixmap(((__file__), "images/")))

    def pasteImage(self):
        clipboard = ()
        (())
    
    def copyHtml(self):
        mimeData = QMimeData()
        ("<b>Bold and<font color=red>Red</font></b>")
        clipboard = ()
        (mimeData)

    def pasteHtml(self):
        clipboard = ()
        mimeData = ()
        if ():
            (())

            
if __name__ == "__main__":
    app = QApplication()
    win = Form()
    ()
    (app.exec_())

to this article on Python PyQt5 drag and drop and clipboard functionality to this article, more related to PyQt5 drag and drop clipboard content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!