[英]PyQt5 Move Button on MainWindow with Drag/Drop
我开始使用PyQt5,在下面的示例中我对mimedata的用法有疑问。
该示例允许通过使用MainWindow上的拖放事件来移动Button。
让我感到困惑的是,在MyButton类中,它设置了QmimeData而不设置任何数据,然后将其直接传递给QDrag对象。
而且我确实尝试删除了QMimedata部分,但这确实使拖放不再起作用,这似乎是必需的设置。
我的问题是这种情况下Qmimedata是什么? 因为根据Qmimedata的描述,现在它应该不再是数据,那么它如何工作?
谢谢!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class MyButton(QtWidgets.QPushButton):
def __init__(self, parent=None):
super().__init__(parent)
def mouseMoveEvent(self, e):
if e.buttons() != QtCore.Qt.RightButton:
return
mimeData = QtCore.QMimeData()
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft())
drag.exec_(QtCore.Qt.MoveAction)
def mousePressEvent(self, e):
super().mousePressEvent(e)
if e.button() == QtCore.Qt.LeftButton:
print('press')
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = MyButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(220, 180, 331, 151))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Move it"))
class Example(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
position = e.pos()
self.ui.pushButton.move(position)
e.setDropAction(QtCore.Qt.MoveAction)
e.accept()
apps = QtWidgets.QApplication(sys.argv)
ex = Example()
ex.show()
apps.exec_()
似乎您正在尝试使用从pyuic
创建的代码,该代码不应进行编辑或“仿制”:应保留原样的编译后的代码,并使用写在不同文件中的实际代码来使用),如解释在这里 。
无论如何,最重要的部分是了解Qt的拖放实现方式。 我建议您仔细阅读官方文档 :虽然它是为C ++开发人员编写的,但是概念是相同的。 它似乎不堪重负,并且某种程度上过于复杂,但是这样做的理由有很多严肃而充分的理由。
基本上,创建拖动对象时,它必须附加一些数据,否则,任何应用程序(包括您自己的应用程序)都不会知道如何处理它。 对于此用例,您至少需要两件事:
这是通过将一些数据附加到以MIME格式设置的QDrag对象来完成的。 由于这是特定情况(有关Qt提供的标准类型,请参见QMimeData文档),因此您需要创建自己的mimeData格式并提供一些数据。 为此,您需要在写入模式下将QByteArray和QDataStream设置为数组,以用于写入拖动对象所需的实际数据。
创建并“执行”拖动对象后,您需要确保接收器dragEnterEvent的事件实际上包含您感兴趣的MIME格式,该格式实际存在于该事件的mimeData中,以便您可以读取其数据并进行实际操作一些有用的东西。
我创建了一个非常基本的示例,允许您使用拖放操作在界面上移动按钮,但是请注意,这具有严重的含义:在这种情况下,只有一个父窗口小部件( centralWidget
),但是如果要使用一些更复杂的布局(可能通过使用框架或组框)将无法正常工作。 向主窗口小部件的子级添加窗口小部件将使后者成为前者的父级,后者将使其位置相对于父级位置。
例如,假设您创建了一个具有两个组框的布局,并且已将按钮添加到第二个中。 在这种情况下,除非将其重新放置,否则将无法轻松将其移至第一个框,并且如果尝试在第二个框(已存在)中移动,则需要根据以下公式计算其最终位置该框,只要您在主窗口中捕获dropEvent即可。 甚至更多:如果您有一些复杂的布局(带有嵌套的小部件和布局),则需要找到一种方法来获取要拖动到的实际小部件。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MyButton(QtWidgets.QPushButton):
def mousePressEvent(self, event):
super().mousePressEvent(event)
if event.button() == QtCore.Qt.LeftButton:
print('press')
elif event.button() == QtCore.Qt.RightButton:
# save the click position to keep it consistent when dragging
self.mousePos = event.pos()
def mouseMoveEvent(self, event):
if event.buttons() != QtCore.Qt.RightButton:
return
mimeData = QtCore.QMimeData()
# create a byte array and a stream that is used to write into
byteArray = QtCore.QByteArray()
stream = QtCore.QDataStream(byteArray, QtCore.QIODevice.WriteOnly)
# set the objectName and click position to keep track of the widget
# that we're moving and it's click position to ensure that it will
# be moved accordingly
stream.writeQString(self.objectName())
stream.writeQVariant(self.mousePos)
# create a custom mimeData format to save the drag info
mimeData.setData('myApp/QtWidget', byteArray)
drag = QtGui.QDrag(self)
# add a pixmap of the widget to show what's actually moving
drag.setPixmap(self.grab())
drag.setMimeData(mimeData)
# set the hotspot according to the mouse press position
drag.setHotSpot(self.mousePos - self.rect().topLeft())
drag.exec_()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
super().__init__()
self.resize(800, 600)
centralWidget = QtWidgets.QWidget()
self.setCentralWidget(centralWidget)
self.pushButton = MyButton(centralWidget, objectName='pushButton')
self.pushButton.setGeometry(QtCore.QRect(220, 180, 331, 151))
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
# only accept our mimeData format, ignoring any other data content
if event.mimeData().hasFormat('myApp/QtWidget'):
event.accept()
def dropEvent(self, event):
stream = QtCore.QDataStream(event.mimeData().data('myApp/QtWidget'))
# QDataStream objects should be read in the same order as they were written
objectName = stream.readQString()
# find the child widget that has the objectName set within the drag event
widget = self.findChild(QtWidgets.QWidget, objectName)
if not widget:
return
# move the widget relative to the original mouse position, so that
# it will be placed exactly where the user drags it and according to
# the original click position
widget.move(event.pos() - stream.readQVariant())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.