亲宝软件园·资讯

展开

Python PyQt5实现拖拽与剪贴板功能详解

SongYuLong的博客 人气:0

拖拽

基于MIME类型的拖拽数据传输时基于QDrag类的QMimeData对象管理的数据与其对应的MIME类型相关联。

MimeData类函数允许检测和使用方法的MIME类型

判断函数设置函数获取函数MIME类型
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

常用拖拽事件

事件描述
DragEnterEvent当执行一个拖拽控件操作,并且鼠标指针进入该控件时被触发
DragMoveEvent在拖拽操作进行时会触发该事件
DragLeaveEvent当执行一个拖拽控件操作,并且鼠标指针离开该控件时被触发
DropEvent当拖拽操作在目标控件上被释放时,触发该事件
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt

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

class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
    
    def initUI(self):
        lo = QFormLayout()
        lo.addRow(QLabel("请把左边的文本拖拽到右边的下拉菜单中"))
        edit = QLineEdit()
        edit.setDragEnabled(True)
        com = Combo("Button", self)
        lo.addRow(edit, com)
        self.setLayout(lo)
        self.setWindowTitle("简单的拖拽例子")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Example()
    win.show()
    sys.exit(app.exec_())

剪贴板

QClipboard类提供了对系统剪贴板的访问,可以在应用程序之间复制和粘贴数据。它的操作类似于QDrag类,并使用类似的数据类型。

QApplication类有一个静态方法clipboard(),返回剪贴板对象的引用。任何类型的MimeData都可以从剪贴板复制或粘贴。

QClipboard常用方法

方法描述
clear()清除剪贴板的内容
setImage()将QImage对象复制到剪贴板中
setMimeData()将MIME数据设置为剪贴板
setPixmap()从剪贴板中复制Pixmap对象
setText()从剪贴板中复制文本
text()从剪贴板中检索文本

QClipboard类中的常用信号

信号含义
dataChanged当剪贴板内容发生变化时触发该信号
import os
import sys
from PyQt5.QtCore import QMimeData
from PyQt5.QtWidgets import (QApplication, QDialog, QGridLayout, QLabel, QPushButton)
from PyQt5.QtGui 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")

        self.textLabel = QLabel("Original text")
        self.imageLabel = QLabel()
        self.imageLabel.setPixmap(QPixmap(os.path.join(os.path.dirname(__file__), "images/clock.png")))

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

        layout.addWidget(textPasteButton, 1, 0)
        layout.addWidget(imagePasteButton, 1, 1)
        layout.addWidget(htmlPasteButton, 1, 2)

        layout.addWidget(self.textLabel, 2, 0, 1, 2)
        layout.addWidget(self.imageLabel, 2, 2)
        self.setLayout(layout)

        textCopyButton.clicked.connect(self.copyText)
        textPasteButton.clicked.connect(self.pasteText)
        imageCopyButton.clicked.connect(self.copyImage)
        imagePasteButton.clicked.connect(self.pasteImage)
        htmlCopyButton.clicked.connect(self.copyHtml)
        htmlPasteButton.clicked.connect(self.pasteHtml)

    def copyText(self):
        print(os.path.join(os.path.dirname(__file__)))
        clipboard = QApplication.clipboard()
        clipboard.setText("I've been clipped")

    def pasteText(self):
        clipboard = QApplication.clipboard()
        self.textLabel.setText(clipboard.text())
    
    def copyImage(self):
        clipboard = QApplication.clipboard()
        clipboard.setPixmap(QPixmap(os.path.join(os.path.dirname(__file__), "images/python.jpg")))

    def pasteImage(self):
        clipboard = QApplication.clipboard()
        self.imageLabel.setPixmap(clipboard.pixmap())
    
    def copyHtml(self):
        mimeData = QMimeData()
        mimeData.setHtml("<b>Bold and<font color=red>Red</font></b>")
        clipboard = QApplication.clipboard()
        clipboard.setMimeData(mimeData)

    def pasteHtml(self):
        clipboard = QApplication.clipboard()
        mimeData = clipboard.mimeData()
        if mimeData.hasHtml():
            self.textLabel.setText(mimeData.html())

            
if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Form()
    win.show()
    sys.exit(app.exec_())

加载全部内容

相关教程
猜你喜欢
用户评论