【PyQt4 实例31】拖拽图标

# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

class DragWidget(QFrame):
    def __init__(self,parent=None):
        super(DragWidget,self).__init__(parent)
        self.setWindowTitle(self.tr("拖拽图标"))
        self.setMinimumSize(400,400)
        self.setAcceptDrops(True)
        self.setFrameStyle(QFrame.Sunken | QFrame.StyledPanel)
        
        icon1 = QLabel(self)
        icon1.setPixmap(QPixmap("image/butterfly.png"))
        icon1.setAttribute(Qt.WA_DeleteOnClose)
        
        icon2 = QLabel(self)
        icon2.setPixmap(QPixmap("image/cc.png"))
        
        icon3 = QLabel(self)
        icon3.setPixmap(QPixmap("image/star.png"))
        
        icon1.move(20,20)
        icon2.move(120,20)
        icon3.move(220,20)
    
    def dragEnterEvent(self, event):
        
        if event.mimeData().hasFormat("Drag-Icon"):
            if event.source() == self:
                event.setDropAction(Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()
        else:
            event.ignore()
    
    dragMoveEvent = dragEnterEvent
                
    def dropEvent(self,e):
        if e.mimeData().hasFormat("Drag-Icon"):
            data = e.mimeData().data("Drag-Icon")
            stream = QDataStream(data,QIODevice.ReadOnly)
            pix = QPixmap()
            offset = QPoint()
            stream >> pix >> offset
            icon = QLabel(self)
            icon.setPixmap(pix)
            icon.move(e.pos() - offset)
            icon.show()
            
            if e.source() == self:
                e.setDropAction(Qt.MoveAction)
                e.accept()
            else:
                e.acceptProposedAction()
        else:
            e.ignore()
            
    def mousePressEvent(self,event):
        child = self.childAt(event.pos())
        if not child:
            return
        
        pixmap = QPixmap(child.pixmap())
        
        itemData = QByteArray()
        dataStream = QDataStream(itemData, QIODevice.WriteOnly)
        dataStream << pixmap << QPoint(event.pos() - child.pos())
        
        mimeData = QMimeData()
        mimeData.setData('Drag-Icon', itemData)
        
        drag = QDrag(self)
        drag.setMimeData(mimeData)
        drag.setPixmap(pixmap)
        drag.setHotSpot(event.pos() - child.pos())
        
        tempPixmap = QPixmap(pixmap)
        painter = QPainter()
        painter.begin(tempPixmap)
        painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127))
        painter.end()
        
        child.setPixmap(tempPixmap)
        
        if drag.exec_(Qt.CopyAction | Qt.MoveAction, Qt.CopyAction) == Qt.MoveAction:
            child.close()
        else:
            child.show()
            child.setPixmap(pixmap)
        
        
app=QApplication(sys.argv)
dialog=DragWidget()
dialog.show()
app.exec_()

你可能感兴趣的:(python)