pyQt 控件拖拽笔记
#!/usr/bin/env python# -*- coding: utf-8 -*-
# Author Cool_Breeze
# Python Version 3.7.9
# OS Windows 10
from PySide2.QtWidgets import QApplication,QFrame,QLabel,QWidget,QHBoxLayout
import PySide2.QtGui
from PySide2.QtGui import QPixmap,QDrag,Qt,QPainter,QColor
from PySide2.QtCore import Qt,QByteArray,QBuffer,QIODevice,QMimeData,QDataStream,QPoint
class MFrame(QFrame):
def __init__(self, parent=None, f=Qt.WindowFlags()):
super(MFrame, self).__init__(parent, f)
self.setAcceptDrops(True)
self.moveMousePos = QPoint(0,0)
def mouseMoveEvent(self, event:PySide2.QtGui.QMouseEvent):
wd: QLabel # 智能提醒
wd = self.childAt(event.localPos().toPoint()) # 获取点击控件
if wd is None:
return super().mousePressEvent(event)
pix = wd.pixmap() # 获取图片内存
arr = QByteArray() # 存储控件
buffer = QBuffer(arr) # 缓冲区
buffer.open(QIODevice.WriteOnly) # 打开模式
pix.save(buffer, "PNG") # 保存信息
mimeData = QMimeData() # 通信数据
mimeData.setData("application/x-dpng", arr) # 通信协议
drag = QDrag(self) # 拖着图标
drag.setMimeData(mimeData)
drag.setPixmap(pix)
drag.setHotSpot(event.localPos().toPoint() - wd.pos())
# 绘制暗部影像
tpix = QPixmap(pix)
t1pix = QPixmap(pix)
painter = QPainter()
painter.begin(tpix)
painter.fillRect(tpix.rect(), QColor(127, 127, 127, 127))
painter.end()
# 给控件套上
wd.setPixmap(tpix)
# 执行拖拽 复制或者移动
if drag.exec_(Qt.CopyAction | Qt.MoveAction, Qt.CopyAction) == Qt.MoveAction:
print('close')
wd.close()
else:
print(f"copy:{self.objectName()}")
print(t1pix)
wd.setPixmap(t1pix)
#
print('end')
super(MFrame, self).mousePressEvent(event)
def dragEnterEvent(self, event:PySide2.QtGui.QDragEnterEvent):
if event.mimeData().hasFormat("application/x-dpng"):
if (event.source() == self): # 设置移动图标
event.setDropAction(Qt.MoveAction)
event.accept()
else:
event.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event:PySide2.QtGui.QDropEvent):
# 放下事件
if event.mimeData().hasFormat("application/x-dpng"):
arr = QByteArray(event.mimeData().data("application/x-dpng"))
buffer = QBuffer(arr)
pix = QPixmap()
pix.loadFromData(arr.data(),'PNG')
la = QLabel(self)
la.setPixmap((pix))
la.show()
pos = QPoint(event.pos().x() - pix.width() // 2, event.pos().y() - pix.height() // 2)
la.move(pos)
if (id(event.source()) == id(self)):
print("放下")
print(self)
event.setDropAction(Qt.MoveAction)
event.accept()
else:
print('外部放下')
event.acceptProposedAction()
else:
event.ignore()
class Damo(QWidget):
def __init__(self, parent:QWidget=None, f=Qt.WindowFlags()):
super(Damo, self).__init__(parent,f)
self.initUI()
self.resize(800, 600)
def initUI(self):
hl = QHBoxLayout(self)
framea = MFrame(self)
framea.setFrameShape(QFrame.Shape.StyledPanel)
framea.setFrameShadow(QFrame.Sunken)
frameb = MFrame(self)
frameb.setFrameShape(QFrame.Shape.StyledPanel)
frameb.setFrameShadow(QFrame.Sunken)
frameb.setObjectName('b')
framea.setObjectName('a')
hl.addWidget(framea)
hl.addWidget(frameb)
la = QLabel(parent=framea)
la.setPixmap(QPixmap("./dolphin.png"))
lb = QLabel(parent=frameb)
lb.setPixmap(QPixmap("./duck.png"))
self.setLayout(hl)
if __name__ == "__main__":
app = QApplication()
d = Damo()
d.show()
app.exec_() 这是干什么? 为什么 def的方法有些加__ 有些不加呢?是c语言操作习惯了吗 咬字分开念 发表于 2022-8-27 18:36
为什么 def的方法有些加__ 有些不加呢?是c语言操作习惯了吗
python类的魔法方法,或者私有方法
页:
[1]