網(wǎng)站首頁 編程語言 正文
拖拽
基于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_())
原文鏈接:https://blog.csdn.net/songyulong8888/article/details/127986260
相關(guān)推薦
- 2022-06-08 ASP.NET?Core中間件_基礎(chǔ)應(yīng)用
- 2023-08-28 vscode里面報:‘xxx‘ is assigned a value but never used
- 2022-05-20 C++實(shí)現(xiàn)商店倉庫管理系統(tǒng)_C 語言
- 2022-10-31 一文搞懂Go語言操作Redis的方法_Golang
- 2022-09-23 Golang分布式應(yīng)用之Redis示例詳解_Golang
- 2022-05-17 springboot打包為jar
- 2022-10-12 sql中exists的基本用法示例_MsSql
- 2022-05-05 基于PyQt5制作數(shù)據(jù)處理小工具_(dá)python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支