pyqt5:为什么 mimeData().text() returns 什么都没有?
pyqt5: why the mimeData().text() returns nothing?
最近学习PyQt5,尝试拖一个QPushButton学习本教程Drag & drop a button widget,并做了一些改进,让按钮的放置更准确,所以补充
mime = e.mimeData().text()
x, y = mime.split(',')
根据@Avaris 对于这个 question,但我发现 e.mimeData().text()
没有返回任何应该是光标相对于按钮的局部坐标的东西,我尝试 print(mime)
,并且得到一个空白行,然后我 print(mime.split(','))
得到 ['']
。
代码如下:
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_()
在 answer of @Avaris 中,您会注意到他们使用 mouseMoveEvent
中的按钮位置设置 mimedata:
mimeData = QtCore.QMimeData()
# simple string with 'x,y'
mimeData.setText('%d,%d' % (e.x(), e.y()))
默认情况下,mimedata 不包含任何内容。你必须自己设置一切!查看 QMimeData
的文档,看看您还能做什么(除了设置任意文本)
最近学习PyQt5,尝试拖一个QPushButton学习本教程Drag & drop a button widget,并做了一些改进,让按钮的放置更准确,所以补充
mime = e.mimeData().text()
x, y = mime.split(',')
根据@Avaris 对于这个 question,但我发现 e.mimeData().text()
没有返回任何应该是光标相对于按钮的局部坐标的东西,我尝试 print(mime)
,并且得到一个空白行,然后我 print(mime.split(','))
得到 ['']
。
代码如下:
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_()
在 answer of @Avaris 中,您会注意到他们使用 mouseMoveEvent
中的按钮位置设置 mimedata:
mimeData = QtCore.QMimeData()
# simple string with 'x,y'
mimeData.setText('%d,%d' % (e.x(), e.y()))
默认情况下,mimedata 不包含任何内容。你必须自己设置一切!查看 QMimeData
的文档,看看您还能做什么(除了设置任意文本)