Lucas W
Lucas W

Reputation: 43

PySide Qt: Drag and drop of an image

The title basically says it all. I want to create an image that I can drag around inside a window. My code so far does all that but for some reason, the image appears really small even though, I used the scaled() function to resize it. Also, if I change the values inside the scaled() function, the image disappears.

from PySide.QtGui import *
from PySide.QtCore import *
import sys


class Label(QLabel):
    def __init__(self, title, parent):
        super(Label, self).__init__(title, parent)
        self.setup()

    def setup(self):
        folder_pic = QPixmap("path")
        folder_size = folder_pic.scaled(64, 64)
        self.label = QLabel(self)
        self.label.setPixmap(folder_size)

    def mouseMoveEvent(self, e):

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

        mimeData = QMimeData(self)

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

        dropAction = drag.start(Qt.MoveAction)

class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.setAcceptDrops(True)
        self.button = Label("", self)
        self.button.move(100, 65)
        self.setWindowTitle('Click or Move')
        self.setGeometry(300, 300, 280, 150)


    def dragEnterEvent(self, e):

        e.accept()

    def dropEvent(self, e):

        position = e.pos()
        self.button.move(position)
        e.setDropAction(Qt.MoveAction)
        e.accept()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    mywin = Example()
    mywin.show()
    sys.exit(app.exec_())

My code is based on this tutorial http://zetcode.com/gui/pysidetutorial/dragdrop/.

Upvotes: 1

Views: 1749

Answers (1)

ekhumoro
ekhumoro

Reputation: 120778

You set a pixmap on the drag object:

drag.setPixmap(self.pixmap())

But for that to work, you will also need this fix:

def setup(self):
    folder_pic = QPixmap("path")
    folder_size = folder_pic.scaled(64, 64)
    self.setPixmap(folder_size)

Upvotes: 2

Related Questions