1

learning PyQt5 recently, I've tried to drag a QPushButton learning this tutorial Drag & drop a button widget, and made some improvements to place the button more accurate, so I add mime = e.mimeData().text() x, y = mime.split(',') according to @Avaris for this question, but I found e.mimeData().text() returned nothing which supposed to be the coordinate of local position of the cursor with respect to the button, i tried to print(mime), and got a blank line with nothing, then i print(mime.split(',')) and got ['']

here's the code:

import sys
from PyQt5.QtWidgets import QPushButton, QWidget, QApplication, QLabel
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
from PyQt5 import QtCore


class Button(QPushButton):

    def __init__(self, title, parent):
        super().__init__(title, parent)

    def mouseMoveEvent(self, e):

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

        mimeData = QMimeData()

        drag = QDrag(self)
        drag.setMimeData(mimeData)

       dropAction = drag.exec_(Qt.MoveAction)

    def mousePressEvent(self, e):

        QPushButton.mousePressEvent(self, e)

        if e.button() == Qt.LeftButton:
            print('press')


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.setAcceptDrops(True)

        self.button = Button('Button', 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()
        mime = e.mimeData().text()
        x, y = mime.split(',')

        #print(mime.split(','))

        self.button.move(position - QtCore.QPoint(int(x), int(y)))

        e.setDropAction(Qt.MoveAction)
        e.accept()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()
Community
  • 1
  • 1
Cesc
  • 35
  • 1
  • 12

2 Answers2

2

In the answer of @Avaris, you will notice they set the mimedata with the button position in the mouseMoveEvent:

mimeData = QtCore.QMimeData()
# simple string with 'x,y'
mimeData.setText('%d,%d' % (e.x(), e.y()))

The mimedata does not contain anything by default. You have to set everything yourself! Have a look at the documentation for QMimeData to see what else you can do (other than setting arbitrary text)

Community
  • 1
  • 1
three_pineapples
  • 11,579
  • 5
  • 38
  • 75
  • Thanks for the answer, it worked, i should have figured it out myself, didn't notice the `mimedata` contains nothing by default. – Cesc Jun 01 '15 at 01:59
  • @CescFangs Happy to help. If this answers your question in full, I'd appreciate it if you could "accept" my answer by clicking on the tick mark on the left hand side of the answer. Cheers! – three_pineapples Jun 01 '15 at 02:41
0

Drag and Drop in Camera View


def dragEnterEvent(self, event):   # Drag lines
        mimeData = QtCore.QMimeData()
        if mimeData.hasText:

            event.accept()
        else:
            event.ignore()

  
def dropEvent(self, event): # Drop lines
  
        mimeData = QtCore.QMimeData()

        format = 'application/x-qabstractitemmodeldatalist'
        data=event.mimeData().data(format)     # Drag Drop get data's name
    
        name_str = codecs.decode(data,'utf-8')  # Convert byte to string
       
        mimeData.setText(name_str)
        # print(name_str[26:].replace('\x00','').strip("")) # remove white space
        
        if mimeData.hasText:
            
            print(name_str)
            # write what you will do


Ugur Baran
  • 108
  • 6