【问题标题】:How to show a QPushButton while it is being dragged with QDrag in PyQt5?如何在 PyQt5 中使用 QDrag 拖动 QPushButton 时显示它?
【发布时间】:2018-05-17 05:54:25
【问题描述】:

当我拖动我的 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')

【问题讨论】:

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


    【解决方案1】:

    你必须通过setPixmap()设置你想要显示的图片,才能得到你应该使用grab()的widget的图片:

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

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多