如何在PyQt5中使用QDrag拖动时显示QPushButton?

时间:2017-12-03 18:50:36

标签: python python-3.x drag-and-drop pyqt pyqt5

当我拖动我的QPushButton时,它会消失直到掉落。我想在拖动时一直显示按钮。怎么做?

向下拖动包含QDrag对象的按钮。

如果需要,很高兴分享更多代码:)

我的屏幕照片

class DraggableCodeBlock(QPushButton):
    def __init__(self, width, height, offset_left, offset_top, parent, command):

        super().__init__(parent=parent)

        self.parent = parent

        self.setText(command)

        self.show()

    def mouseMoveEvent(self, e):

        if e.buttons() != Qt.LeftButton:
            return

        mimeData = QMimeData()

        drag = QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(e.pos() - self.rect().topLeft())

        dropAction = drag.exec_(Qt.MoveAction)
        super(DraggableCodeBlock, self).mouseMoveEvent(e)

    def mousePressEvent(self, e):

        super().mousePressEvent(e)

        if e.button() == Qt.LeftButton or not(self.is_mobile):
            print('press')

1 个答案:

答案 0 :(得分:1)

您必须设置要通过setPixmap()显示的图片,以获取您应该使用的小部件的图像grab()

drag = QDrag(self)
drag.setPixmap(self.grab())