python 3.x - pyqt5: why the mimeData().text() returns nothing? -
learning pyqt5 recently, i've tried drag qpushbutton learning tutorial drag & drop button widget, , made improvements place button more accurate, add mime = e.mimedata().text() x, y = mime.split(',')
according @avaris question, found e.mimedata().text()
returned nothing supposed coordinate of local position of cursor respect button, tried print(mime)
, , got blank line nothing, print(mime.split(','))
, got ['']
。
here's code:
import sys pyqt5.qtwidgets import qpushbutton, qwidget, qapplication, qlabel pyqt5.qtcore import qt, qmimedata pyqt5.qtgui import qdrag 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_()
in answer of @avaris, notice set mimedata button position in mousemoveevent
:
mimedata = qtcore.qmimedata() # simple string 'x,y' mimedata.settext('%d,%d' % (e.x(), e.y()))
the mimedata not contain default. have set yourself! have @ documentation qmimedata
see else can (other setting arbitrary text)
Comments
Post a Comment