吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 800|回复: 3
收起左侧

[学习记录] pyQt 控件拖拽笔记

[复制链接]
Cool_Breeze 发表于 2022-8-27 14:21
[Python] 纯文本查看 复制代码
#!/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_()
QQ截图20220827142105.png

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

canso123 发表于 2022-8-27 15:30
这是干什么?
咬字分开念 发表于 2022-8-27 18:36
为什么 def  的方法有些加__ 有些不加呢?是c语言操作习惯了吗
 楼主| Cool_Breeze 发表于 2022-8-27 18:41
咬字分开念 发表于 2022-8-27 18:36
为什么 def  的方法有些加__ 有些不加呢?是c语言操作习惯了吗

python类的魔法方法,或者私有方法

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
咬字分开念 + 1 + 1 了解了,懒人就是懒得谷歌的人

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 01:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表