【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_()

發佈了62 篇原創文章 · 獲贊 9 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章