开发者

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

目录
  • 拖拽
  • 剪贴板

拖拽

基于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_())

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

剪贴板

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.javascriptsetLayout(layouhttp://www.devze.comt)

        textCopyButton.clicked.connect(self.copyText)
        textPasteButton.clicked.connect(self.pasteText)
        imageCopyButton.clicked.conhttp://www.devze.comnect(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 pasthttp://www.devze.comeText(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("Bold and<font color=red>Red</font>")
        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_())

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

到此这篇关于Python PyQt5实现拖拽与剪贴板功能详解的文章就介绍到这了,更多相关PyQt5拖拽 剪贴板内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜