pyqt5中的可拖动QLabels

问题描述

我正在python 3.7.x中使用pyqt5和qt设计器,但是我可拖动的QLabels似乎不起作用。我试图将de Labels设置为可拖动并将其拖放到另一个Label中,但是我的代码不起作用,我在qt designer中创建了窗口,还在其中定义了Lebels,但是当我尝试将QLabels转换为可拖动的对象什么也没有发生。这是我的一些代码,我将解释:

class Window(window_name,base_class):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.penguin_list = (
            self.cyan,self.red,self.yellow,self.purple,self.green
        )  # This tuple is made out of the QLabel created on qt designer that i want to drag 

    def mousePressEvent(self,event):
        # Here I tried to set the QLabel that I want to drag (the one the mouse pressed)
        if event.button() == Qt.LeftButton:
            for penguin in self.penguin_list:
                if penguin.underMouse():
                    self.penguin = penguin
                    self.drag_start_position = event.pos() 

    def mouseMoveEvent(self,event):
        # Here is when I tried to convert the QLavel into draggable
        # Also the QLabel appears in the top left corner (sometimes) with a white background
        # And also the drop part doesn't work and I can not drop the QLabel
        if not (event.buttons() & Qt.LeftButton):
            return
        if (event.pos() - self.drag_start_position).manhattanLength() < QApplication.startDragDistance():
            return

        drag = QDrag(self.penguin)
        mimedata = QMimeData()
        mimedata.setImageData(self.penguin.pixmap())

        drag.setMimeData(mimedata)
        pixmap = QPixmap(self.penguin.size())
        painter = QPainter(pixmap)
        painter.drawPixmap(self.penguin.rect(),self.penguin.grab())
        painter.end()
        drag.setPixmap(pixmap)
        drag.setHotSpot(event.pos())
        drag.exec_(Qt.CopyAction | Qt.MoveAction)

总体上,我想复制QLabel并将其拖放(粘贴)到其他QFrame中的其他QLabel中。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)