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

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

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

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

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

简单的拖放

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

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

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

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

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

  1. self.setAcceptDrops(True)

我们为小部件启用放置事件。

  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 = QtGui.QLineEdit('', self)
  2. edit.setDragEnabled(True)

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

PyQt4 中的拖放 - 图1

图:简单 drag & drop

拖动和放置一个按钮小部件

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

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt4 tutorial
  5. In this program, we can press on a button
  6. with a left mouse click or drag and drop the
  7. button with the right mouse click.
  8. author: Jan Bodnar
  9. website: zetcode.com
  10. last edited: October 2013
  11. """
  12. import sys
  13. from PyQt4 import QtCore, QtGui
  14. class Button(QtGui.QPushButton):
  15. def __init__(self, title, parent):
  16. super(Button, self).__init__(title, parent)
  17. def mouseMoveEvent(self, e):
  18. if e.buttons() != QtCore.Qt.RightButton:
  19. return
  20. mimeData = QtCore.QMimeData()
  21. drag = QtGui.QDrag(self)
  22. drag.setMimeData(mimeData)
  23. drag.setHotSpot(e.pos() - self.rect().topLeft())
  24. dropAction = drag.start(QtCore.Qt.MoveAction)
  25. def mousePressEvent(self, e):
  26. super(Button, self).mousePressEvent(e)
  27. if e.button() == QtCore.Qt.LeftButton:
  28. print 'press'
  29. class Example(QtGui.QWidget):
  30. def __init__(self):
  31. super(Example, self).__init__()
  32. self.initUI()
  33. def initUI(self):
  34. self.setAcceptDrops(True)
  35. self.button = Button('Button', self)
  36. self.button.move(100, 65)
  37. self.setWindowTitle('Click or Move')
  38. self.setGeometry(300, 300, 280, 150)
  39. self.show()
  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(QtCore.Qt.MoveAction)
  46. e.accept()
  47. def main():
  48. app = QtGui.QApplication([])
  49. ex = Example()
  50. sys.exit(app.exec_())
  51. if __name__ == '__main__':
  52. main()

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

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

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

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

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

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

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

  1. dropAction = drag.start(QtCore.Qt.MoveAction)

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

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

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

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

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

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

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

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