如何在QFrame中设置QDrag缩略图的位置?

时间:2018-10-18 15:35:46

标签: python pyqt pyqt5

在我的QFrame对象中,我试图实现在鼠标按下和鼠标移动时缩略图跟随光标位置的效果。我设法实现了这一目标。但是,缩略图具有某种动画形式,它将从0,0对象的QFrame“飞入”到光标所在的位置,并在鼠标上返回0,0释放。

看起来像这样:

enter image description here

import sys
from PyQt5.QtWidgets import QApplication, QFrame
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag, QPixmap

class Example(QFrame):

    def __init__(self):
        super().__init__()
        self.resize(500, 500)

    def mouseMoveEvent(self, e):
        mimeData = QMimeData()
        drag = QDrag(self)
        thumbnail = QPixmap('test.png').scaled(100, 100, Qt.KeepAspectRatio)
        drag.setPixmap(thumbnail)
        drag.setMimeData(mimeData)
        drag.setHotSpot(e.pos())
        drag.exec_(Qt.MoveAction)

if __name__ == '__main__':    
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()

应该添加/更改哪些内容,以使缩略图直接显示在光标的位置,而不是“飞进”和“飞出”?

1 个答案:

答案 0 :(得分:0)

尝试一下:

import sys
from PyQt5.QtWidgets import QApplication, QFrame, QLabel
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag, QPixmap

class Example(QFrame): 
    def __init__(self):
        super().__init__()
        self.resize(500, 500)
        self.label = QLabel(self)     # +++
        self.label.resize(100, 100)
        self.setAcceptDrops(True)     # +++

    def mouseMoveEvent(self, e):
        mimeData = QMimeData()
        drag     = QDrag(self)
        self.thumbnail = QPixmap('E:/_Qt/img/qt-logo.png').scaled(100, 100, Qt.KeepAspectRatio)
        drag.setPixmap(self.thumbnail)
        drag.setMimeData(mimeData)
#        drag.setHotSpot(e.pos())  
        drag.exec_(Qt.MoveAction) 

    def dropEvent(self, e):           # +++
        self.label.setPixmap(self.thumbnail)
        self.label.move(e.pos())

    def dragEnterEvent(self, e):      # +++
        if e.mimeData():
            e.acceptProposedAction()

if __name__ == '__main__':    
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()

enter image description here