Anton_C
Anton_C

Reputation: 23

How to show a QPushButton while it is being dragged with QDrag in PyQt5?

When I drag my QPushButton, it disappears until dropped. I want to show the button all the time while it is being dragged. How to do that?

Downwards my Button that is being dragged around including the QDrag object.

Happy to share more code if needed :)

A photo of my screen

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')

Upvotes: 2

Views: 274

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

you must set the image you want to show through setPixmap(), to get the image of the widget you should use grab():

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

Upvotes: 1

Related Questions