欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python PyQt5實(shí)現(xiàn)拖拽與剪貼板功能詳解

 更新時間:2022年12月01日 15:07:20   作者:SongYuLong的博客  
這篇文章主要為大家詳細(xì)介紹了Python PyQt5如何實(shí)現(xiàn)拖拽與剪貼板功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

拖拽

基于MIME類型的拖拽數(shù)據(jù)傳輸時基于QDrag類的QMimeData對象管理的數(shù)據(jù)與其對應(yīng)的MIME類型相關(guān)聯(lián)。

MimeData類函數(shù)允許檢測和使用方法的MIME類型

判斷函數(shù)設(shè)置函數(shù)獲取函數(shù)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當(dāng)執(zhí)行一個拖拽控件操作,并且鼠標(biāo)指針進(jìn)入該控件時被觸發(fā)
DragMoveEvent在拖拽操作進(jìn)行時會觸發(fā)該事件
DragLeaveEvent當(dāng)執(zhí)行一個拖拽控件操作,并且鼠標(biāo)指針離開該控件時被觸發(fā)
DropEvent當(dāng)拖拽操作在目標(biāo)控件上被釋放時,觸發(fā)該事件
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類提供了對系統(tǒng)剪貼板的訪問,可以在應(yīng)用程序之間復(fù)制和粘貼數(shù)據(jù)。它的操作類似于QDrag類,并使用類似的數(shù)據(jù)類型。

QApplication類有一個靜態(tài)方法clipboard(),返回剪貼板對象的引用。任何類型的MimeData都可以從剪貼板復(fù)制或粘貼。

QClipboard常用方法

方法描述
clear()清除剪貼板的內(nèi)容
setImage()將QImage對象復(fù)制到剪貼板中
setMimeData()將MIME數(shù)據(jù)設(shè)置為剪貼板
setPixmap()從剪貼板中復(fù)制Pixmap對象
setText()從剪貼板中復(fù)制文本
text()從剪貼板中檢索文本

QClipboard類中的常用信號

信號含義
dataChanged當(dāng)剪貼板內(nèi)容發(fā)生變化時觸發(fā)該信號
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_())

到此這篇關(guān)于Python PyQt5實(shí)現(xiàn)拖拽與剪貼板功能詳解的文章就介紹到這了,更多相關(guān)PyQt5拖拽 剪貼板內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論