Python PyQt5實(shí)現(xiàn)拖拽與剪貼板功能詳解
拖拽
基于MIME類型的拖拽數(shù)據(jù)傳輸時(shí)基于QDrag類的QMimeData對(duì)象管理的數(shù)據(jù)與其對(duì)應(yīng)的MIME類型相關(guān)聯(lián)。
MimeData類函數(shù)允許檢測(cè)和使用方法的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í)行一個(gè)拖拽控件操作,并且鼠標(biāo)指針進(jìn)入該控件時(shí)被觸發(fā) |
| DragMoveEvent | 在拖拽操作進(jìn)行時(shí)會(huì)觸發(fā)該事件 |
| DragLeaveEvent | 當(dāng)執(zhí)行一個(gè)拖拽控件操作,并且鼠標(biāo)指針離開該控件時(shí)被觸發(fā) |
| DropEvent | 當(dāng)拖拽操作在目標(biāo)控件上被釋放時(shí),觸發(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("請(qǐng)把左邊的文本拖拽到右邊的下拉菜單中"))
edit = QLineEdit()
edit.setDragEnabled(True)
com = Combo("Button", self)
lo.addRow(edit, com)
self.setLayout(lo)
self.setWindowTitle("簡(jiǎn)單的拖拽例子")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Example()
win.show()
sys.exit(app.exec_())
剪貼板
QClipboard類提供了對(duì)系統(tǒng)剪貼板的訪問,可以在應(yīng)用程序之間復(fù)制和粘貼數(shù)據(jù)。它的操作類似于QDrag類,并使用類似的數(shù)據(jù)類型。
QApplication類有一個(gè)靜態(tài)方法clipboard(),返回剪貼板對(duì)象的引用。任何類型的MimeData都可以從剪貼板復(fù)制或粘貼。
QClipboard常用方法
| 方法 | 描述 |
|---|---|
| clear() | 清除剪貼板的內(nèi)容 |
| setImage() | 將QImage對(duì)象復(fù)制到剪貼板中 |
| setMimeData() | 將MIME數(shù)據(jù)設(shè)置為剪貼板 |
| setPixmap() | 從剪貼板中復(fù)制Pixmap對(duì)象 |
| setText() | 從剪貼板中復(fù)制文本 |
| text() | 從剪貼板中檢索文本 |
QClipboard類中的常用信號(hào)
| 信號(hào) | 含義 |
|---|---|
| dataChanged | 當(dāng)剪貼板內(nèi)容發(fā)生變化時(shí)觸發(fā)該信號(hào) |
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
TensorFlow實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)CNN
這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)CNN,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
python XlsxWriter模塊創(chuàng)建aexcel表格的實(shí)例講解
今天小編就為大家分享一篇python XlsxWriter模塊創(chuàng)建aexcel表格的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
基于Python編寫一個(gè)自動(dòng)關(guān)機(jī)程序
這篇文章主要介紹了基于Python編寫的一個(gè)自動(dòng)關(guān)機(jī)程序,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的同學(xué)可以學(xué)習(xí)一下2022-01-01
Python和JS反爬之解決反爬參數(shù)?signKey
這篇文章主要介紹了Python和JS反爬之解決反爬參數(shù)?signKey,Python?反爬中有一大類,叫做字體反爬,核心的理論就是通過字體文件或者?CSS?偏移,接下來文章的詳細(xì)介紹,需要的小伙伴可以參考一下2022-05-05
python 遍歷列表提取下標(biāo)和值的實(shí)例
今天小編就為大家分享一篇python 遍歷列表提取下標(biāo)和值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python Django框架介紹之模板標(biāo)簽及模板的繼承
今天給大家?guī)鞵ython Django框架的相關(guān)知識(shí),文中對(duì)模板標(biāo)簽及模板的繼承介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05

