原文: http://zetcode.com/gui/pyqt5/dragdrop/

在 PyQt5 教程的这一部分中,我们将讨论拖放操作。

在计算机图形用户界面中,拖放是单击虚拟对象并将其拖动到其他位置或另一个虚拟对象上的动作(或支持以下动作)。 通常,它可用于调用多种动作,或在两个抽象对象之间创建各种类型的关联。

拖放是图形用户界面的一部分。 拖放操作使用户可以直观地执行复杂的操作。

通常,我们可以拖放两件事:数据或某些图形对象。 如果将图像从一个应用拖到另一个应用,则会拖放二进制数据。 如果我们在 Firefox 中拖动选项卡并将其移动到另一个位置,则将拖放图形组件。

QDrag

QDrag支持基于 MIME 的拖放数据传输。 它处理拖放操作的大多数细节。 传输的数据包含在QMimeData对象中。

简单的拖放

在第一个示例中,我们有一个QLineEdit和一个QPushButton。 我们将纯文本从行编辑小部件中拖放到按钮小部件上。 按钮的标签将更改。

simpledragdrop.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. This is a simple drag and
  6. drop example.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. from PyQt5.QtWidgets import (QPushButton, QWidget,
  12. QLineEdit, QApplication)
  13. import sys
  14. class Button(QPushButton):
  15. def __init__(self, title, parent):
  16. super().__init__(title, parent)
  17. self.setAcceptDrops(True)
  18. def dragEnterEvent(self, e):
  19. if e.mimeData().hasFormat('text/plain'):
  20. e.accept()
  21. else:
  22. e.ignore()
  23. def dropEvent(self, e):
  24. self.setText(e.mimeData().text())
  25. class Example(QWidget):
  26. def __init__(self):
  27. super().__init__()
  28. self.initUI()
  29. def initUI(self):
  30. edit = QLineEdit('', self)
  31. edit.setDragEnabled(True)
  32. edit.move(30, 65)
  33. button = Button("Button", self)
  34. button.move(190, 65)
  35. self.setWindowTitle('Simple drag and drop')
  36. self.setGeometry(300, 300, 300, 150)
  37. if __name__ == '__main__':
  38. app = QApplication(sys.argv)
  39. ex = Example()
  40. ex.show()
  41. app.exec_()

该示例展示了一个简单的拖动&放下操作。

  1. class Button(QPushButton):
  2. def __init__(self, title, parent):
  3. super().__init__(title, parent)
  4. ...

为了在QPushButton小部件上放置文本,我们必须重新实现一些方法。 因此,我们创建了自己的Button类,该类将从QPushButton类继承。

  1. self.setAcceptDrops(True)

我们使用setAcceptDrops()为小部件启用放置事件。

  1. def dragEnterEvent(self, e):
  2. if e.mimeData().hasFormat('text/plain'):
  3. e.accept()
  4. else:
  5. e.ignore()

首先,我们重新实现dragEnterEvent()方法。 我们告知我们接受的数据类型。 在我们的情况下,它是纯文本。

  1. def dropEvent(self, e):
  2. self.setText(e.mimeData().text())

通过重新实现dropEvent()方法,我们定义了放置事件发生了什么。 在这里,我们更改按钮小部件的文本。

  1. edit = QLineEdit('', self)
  2. edit.setDragEnabled(True)

QLineEdit小部件具有对拖动操作的内置支持。 我们需要做的就是调用setDragEnabled()方法来激活它。

PyQt5 拖放 - 图1

图:简单 drag and drop

拖放按钮小部件

在下面的示例中,我们将演示如何拖放按钮小部件。

dragbutton.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this program, we can press on a button with a left mouse
  6. click or drag and drop the button with the right mouse click.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. from PyQt5.QtWidgets import QPushButton, QWidget, QApplication
  12. from PyQt5.QtCore import Qt, QMimeData
  13. from PyQt5.QtGui import QDrag
  14. import sys
  15. class Button(QPushButton):
  16. def __init__(self, title, parent):
  17. super().__init__(title, parent)
  18. def mouseMoveEvent(self, e):
  19. if e.buttons() != Qt.RightButton:
  20. return
  21. mimeData = QMimeData()
  22. drag = QDrag(self)
  23. drag.setMimeData(mimeData)
  24. drag.setHotSpot(e.pos() - self.rect().topLeft())
  25. dropAction = drag.exec_(Qt.MoveAction)
  26. def mousePressEvent(self, e):
  27. super().mousePressEvent(e)
  28. if e.button() == Qt.LeftButton:
  29. print('press')
  30. class Example(QWidget):
  31. def __init__(self):
  32. super().__init__()
  33. self.initUI()
  34. def initUI(self):
  35. self.setAcceptDrops(True)
  36. self.button = Button('Button', self)
  37. self.button.move(100, 65)
  38. self.setWindowTitle('Click or Move')
  39. self.setGeometry(300, 300, 280, 150)
  40. def dragEnterEvent(self, e):
  41. e.accept()
  42. def dropEvent(self, e):
  43. position = e.pos()
  44. self.button.move(position)
  45. e.setDropAction(Qt.MoveAction)
  46. e.accept()
  47. if __name__ == '__main__':
  48. app = QApplication(sys.argv)
  49. ex = Example()
  50. ex.show()
  51. app.exec_()

在我们的代码示例中,窗口上有一个QPushButton。 如果我们用鼠标左键单击该按钮,则会在控制台上显示'press'消息。 通过右键单击并移动按钮,我们可以对按钮小部件执行拖放操作。

  1. class Button(QPushButton):
  2. def __init__(self, title, parent):
  3. super().__init__(title, parent)

我们创建一个从QPushButton派生的Button类。 我们还重新实现了QPushButton的两种方法:mouseMoveEvent()mousePressEvent()mouseMoveEvent()方法是拖放操作开始的地方。

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

在这里,我们决定只能用鼠标右键执行拖放操作。 鼠标左键保留用于单击该按钮。

  1. mimeData = QMimeData()
  2. drag = QDrag(self)
  3. drag.setMimeData(mimeData)
  4. drag.setHotSpot(e.pos() - self.rect().topLeft())

QDrag对象已创建。 该类提供对基于 MIME 的拖放数据传输的支持。

  1. dropAction = drag.exec_(Qt.MoveAction)

拖动对象的exec_()方法开始拖放操作。

  1. def mousePressEvent(self, e):
  2. super().mousePressEvent(e)
  3. if e.button() == Qt.LeftButton:
  4. print('press')

如果我们用鼠标左键单击按钮,我们将在控制台上打印'press'。 注意,我们也在父对象上调用了mousePressEvent()方法。 否则,我们将看不到按钮被按下。

  1. position = e.pos()
  2. self.button.move(position)

dropEvent()方法中,我们指定释放鼠标按钮并完成放置操作后发生的情况。 在本例中,我们找出当前鼠标指针的位置并相应地移动按钮。

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

我们用setDropAction()指定放置动作的类型。 在我们的情况下,这是一个动作。

PyQt5 教程的这一部分专门用于拖放操作。