⇤ ← Revision 1 as of 2008-07-20 07:27:47
Size: 10680
Comment:
|
Size: 10630
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 4: | Line 4: |
''' Drag and Drop in PyQt4 PyQt4中的拖放操作 ''' ::-- ["zhuyj"] [[[DateTime(2008-07-20T07:27:47Z)]]] [[TableOfContents]] |
''' Drag and Drop in PyQt4 PyQt4中的拖放操作 ''' ::-- ["zhuyj"] [[[DateTime(2008-07-20T07:27:47Z)]]] [[TableOfContents]] |
Line 13: | Line 10: |
这部分PyQt4教程我们将了解拖放操作。 '' In computer graphical user interfaces, drag-and-drop is the action of (or support for the action of) clicking on a virtual object and dragging it to a different location or onto another virtual object. In general, it can be used to invoke many kinds of actions, or create various types of associations between two abstract objects. (Wikipedia)'' 在计算机图形用户界面中,拖放操作是指点击一个虚拟对象并把它拖到一个不同的位置或其他的虚拟对象上的操作(或支持的行为).一般来说,可以调用多种操作,或创建两个抽象对象间的特定类型的关联。(Wikipedia) '' Drag and drop functionality is one of the most visible aspects of the graphical user interface. Drag and drop operation enables users to do complex things intuitively. '' 拖放功能是最明显的图形用户接口外观,拖放操作允许用户直观的组合事物。 '' Usually, we can drag and drop two things. Data or some graphical objects. If we drag an image from one application to another, we drag and drop binary data. If we drag a tab in Firefox and move it to another place, we drag and drop a graphical component. '' 一般来说,我们可以拖放两件事情,数据或依稀图形对象。如果我们将一幅图像从一个应用程序拖到另一个,我们拖放的是二进制数据。如果我们拖起一个火狐的标签并将它放到另一个地方,我们拖放的是图形组件。 |
这部分PyQt4教程我们将了解拖放操作。 '' In computer graphical user interfaces, drag-and-drop is the action of (or support for the action of) clicking on a virtual object and dragging it to a different location or onto another virtual object. In general, it can be used to invoke many kinds of actions, or create various types of associations between two abstract objects. (Wikipedia)'' 在计算机图形用户界面中,拖放操作是指点击一个虚拟对象并把它拖到一个不同的位置或其他的虚拟对象上的操作(或支持的行为).一般来说,可以调用多种操作,或创建两个抽象对象间的特定类型的关联。(Wikipedia) '' Drag and drop functionality is one of the most visible aspects of the graphical user interface. Drag and drop operation enables users to do complex things intuitively. '' 拖放功能是最明显的图形用户接口外观,拖放操作允许用户直观的组合事物。 ''Usually, we can drag and drop two things. Data or some graphical objects. If we drag an image from one application to another, we drag and drop binary data. If we drag a tab in Firefox and move it to another place, we drag and drop a graphical component. '' 一般来说,我们可以拖放两件事情,数据或依稀图形对象。如果我们将一幅图像从一个应用程序拖到另一个,我们拖放的是二进制数据。如果我们拖起一个火狐的标签并将它放到另一个地方,我们拖放的是图形组件。 |
Line 22: | Line 29: |
In the first example, we will have a QLineEdit and a QPushButton. We will drag plain text from the line edit widget and drop it onto the button widget. 第一个例子中,我们有一个QLineEdit和一个QPushButton。我们将从行编辑组件中拖动无格式的文本并将它放到按钮组件上。 {{{#!python |
. In the first example, we will have a QLineEdit and a QPushButton. We will drag plain text from the line edit widget and drop it onto the button widget. 第一个例子中,我们有一个QLineEdit和一个QPushButton。我们将从行编辑组件中拖动无格式的文本并将它放到按钮组件上。 {{{ #!python |
Line 26: | Line 36: |
Line 28: | Line 37: |
Line 31: | Line 39: |
Line 36: | Line 43: |
Line 41: | Line 47: |
event.ignore() | event.ignore() |
Line 44: | Line 49: |
self.setText(event.mimeData().text()) | self.setText(event.mimeData().text()) |
Line 50: | Line 53: |
Line 53: | Line 55: |
Line 57: | Line 58: |
Line 60: | Line 60: |
Line 64: | Line 64: |
self.move((screen.width()-size.width())/2, | self.move((screen.width()-size.width())/2, |
Line 66: | Line 66: |
Line 72: | Line 71: |
{{{#!python |
{{{ #!python |
Line 78: | Line 77: |
In order to drop text on the QPushButton widget, we must reimplement some methods. So we create our own Button class, which will inherit from the QPushButton widget. 为了将文本放到QPushButton组件,我们必须重载一些方法,所以我们通过继承QPushButton组件来创建我们自己的按钮类。 |
. In order to drop text on the QPushButton widget, we must reimplement some methods. So we create our own Button class, which will inherit from the QPushButton widget. 为了将文本放到QPushButton组件,我们必须重载一些方法,所以我们通过继承QPushButton组件来创建我们自己的按钮类。 |
Line 83: | Line 83: |
We enable drop events for the QPushButton widget. 我们允许QPushButton的放置事件。 {{{#!python |
. We enable drop events for the QPushButton widget. 我们允许QPushButton的放置事件。 {{{ #!python |
Line 90: | Line 92: |
event.ignore() }}} First we reimplement the dragEnteEvent() method. We inform about the data type, we will accept. In our case it is plain text. 首先我们重载dragEnteEvent()方法,我们通知我们将要收到的数据类型,这里是无格式文本。 {{{#!python |
event.ignore() }}} . First we reimplement the dragEnteEvent() method. We inform about the data type, we will accept. In our case it is plain text. 首先我们重载dragEnteEvent()方法,我们通知我们将要收到的数据类型,这里是无格式文本。 {{{ #!python |
Line 96: | Line 100: |
self.setText(event.mimeData().text()) }}} By reimplementing the dropEvent() method, we will define, what we will do upon the drop event. Here we change the text of the button widget. 通过重载dropEvent()方法,我们定义我们收到drop事件后如何操作,这里我们改变按钮组件显示的文本。 |
self.setText(event.mimeData().text()) }}} . By reimplementing the dropEvent() method, we will define, what we will do upon the drop event. Here we change the text of the button widget. 通过重载dropEvent()方法,我们定义我们收到drop事件后如何操作,这里我们改变按钮组件显示的文本。 |
Line 104: | Line 109: |
The QLineEdit widget has a built-in support for drag operations. All we need to do is to call setDragEnabled() method to activate it. QLineEdit组件有内置的拖动操作,我们所要作的就是调用setDragEnabled()方法来接收它。 '' Simple Drag & Drop '' Figure: Simple Drag & Drop |
. The QLineEdit widget has a built-in support for drag operations. All we need to do is to call setDragEnabled() method to activate it. QLineEdit组件有内置的拖动操作,我们所要作的就是调用setDragEnabled()方法来接收它。 '' Simple Drag & Drop '' Figure: Simple Drag & Drop |
Line 111: | Line 116: |
In the following example, we will demonstrate, how to drag & drop a button widget. 以下的例子中我们将示范如何拖放一个按钮组件。 {{{#!python |
. In the following example, we will demonstrate, how to drag & drop a button widget. 以下的例子中我们将示范如何拖放一个按钮组件。 {{{ #!python |
Line 115: | Line 123: |
Line 117: | Line 124: |
Line 121: | Line 127: |
Line 125: | Line 130: |
Line 127: | Line 131: |
Line 130: | Line 134: |
Line 132: | Line 135: |
Line 136: | Line 138: |
Line 138: | Line 139: |
Line 141: | Line 141: |
Line 146: | Line 145: |
Line 152: | Line 148: |
Line 156: | Line 151: |
Line 159: | Line 153: |
Line 163: | Line 157: |
self.move((screen.width()-size.width())/2, | self.move((screen.width()-size.width())/2, |
Line 165: | Line 159: |
Line 169: | Line 161: |
Line 171: | Line 162: |
Line 176: | Line 166: |
Line 179: | Line 168: |
Line 186: | Line 173: |
In our code example, we have a QPushButton on the window. If we click on the button with a left mouse button, we print 'press' to the console. By right clicking and moving the button, we perform a drag & drop operation on the button widget. 在我们的例子里,窗口有一个QPushButton,如果我们鼠标左键点击按钮,我们在终端上打印'press',如果我们右键点击并拖动按钮,我们对按钮组件执行拖放操作。 {{{#!python |
. In our code example, we have a QPushButton on the window. If we click on the button with a left mouse button, we print 'press' to the console. By right clicking and moving the button, we perform a drag & drop operation on the button widget. 在我们的例子里,窗口有一个QPushButton,如果我们鼠标左键点击按钮,我们在终端上打印'press',如果我们右键点击并拖动按钮,我们对按钮组件执行拖放操作。 {{{ #!python |
Line 193: | Line 182: |
We create a Button class, which will derive from the QPushButton. We also reimplement two methods of the QPushButton. mouseMoveEvent() and mousePressEvent(). The mouseMoveEvent() method is the place, where the drag & drop operation begins. 我们创建一个起源自QPushButton的按钮类。我们同时重载QPushButton的两个方法mouseMoveEvent()和mousePressEvent()。mouseMoveEvent()方法是拖放操作开始的位置。 |
. We create a Button class, which will derive from the QPushButton. We also reimplement two methods of the QPushButton. mouseMoveEvent() and mousePressEvent(). The mouseMoveEvent() method is the place, where the drag & drop operation begins. 我们创建一个起源自QPushButton的按钮类。我们同时重载QPushButton的两个方法mouseMoveEvent()和mousePressEvent()。mouseMoveEvent()方法是拖放操作开始的位置。 |
Line 199: | Line 189: |
Here we decide, that we can perform drag & drop only with a right mouse button. The left mouse button is reserved for clicking on the button. 这里我们设定只对鼠标右键的拖放操作作出响应,对鼠标左键操作保留给点击按钮。 |
. Here we decide, that we can perform drag & drop only with a right mouse button. The left mouse button is reserved for clicking on the button. 这里我们设定只对鼠标右键的拖放操作作出响应,对鼠标左键操作保留给点击按钮。 |
Line 203: | Line 194: |
Line 208: | Line 198: |
Here we create a QDrag object. 这里我们创建一个QDrag对象。 |
. Here we create a QDrag object. 这里我们创建一个QDrag对象。 |
Line 212: | Line 203: |
Line 216: | Line 206: |
The start() method of the drag object starts the drag & drop operation. If we perform a move drop action, we destory the button widget. Technically, we destroy a widget on the current position and recreate it on a new position. 拖动对象的start()方法开始拖放操作。如果我们完成一次移动放置操作,我们要销毁按钮组件。技术上来讲,我们在当前位置销毁一个组件,并在新位置重新创建它。 {{{#!python |
. The start() method of the drag object starts the drag & drop operation. If we perform a move drop action, we destory the button widget. Technically, we destroy a widget on the current position and recreate it on a new position. 拖动对象的start()方法开始拖放操作。如果我们完成一次移动放置操作,我们要销毁按钮组件。技术上来讲,我们在当前位置销毁一个组件,并在新位置重新创建它。 {{{ #!python |
Line 224: | Line 216: |
We print 'press' to the console, if we left click on the button with the mouse. Notice that we call mousePressEvent() method on the parent as well. Otherwise we would not see the button being pushed. 如果我们点击鼠标左键,就在控制台打印'press'。注意,这里我们调用了父类的mousePressEvent()方法,否则我们无法看到鼠标被按下的效果。 |
. We print 'press' to the console, if we left click on the button with the mouse. Notice that we call mousePressEvent() method on the parent as well. Otherwise we would not see the button being pushed. 如果我们点击鼠标左键,就在控制台打印'press'。注意,这里我们调用了父类的mousePressEvent()方法,否则我们无法看到鼠标被按下的效果。 |
Line 232: | Line 225: |
In the dropEvent() method we code, what happens after we release the mouse button and finish the drop operation. In our example, we create a new Button widget at the current position of the mouse pointer. 在dropEvent()方法中包含了当我们释放鼠标按键并且结束放置后的操作的代码。在我们的例子里,我们在鼠标指针的当前位置创建了一个新的按钮组件。 |
. In the dropEvent() method we code, what happens after we release the mouse button and finish the drop operation. In our example, we create a new Button widget at the current position of the mouse pointer. 在dropEvent()方法中包含了当我们释放鼠标按键并且结束放置后的操作的代码。在我们的例子里,我们在鼠标指针的当前位置创建了一个新的按钮组件。 |
Line 238: | Line 232: |
We specify the type of the drop action. In our case it is a move action. 我们指定释放操作的类型。这里是移动操作。 |
. We specify the type of the drop action. In our case it is a move action. 我们指定释放操作的类型。这里是移动操作。 |
Drag and Drop in PyQt4 PyQt4中的拖放操作
::-- ["zhuyj"] [DateTime(2008-07-20T07:27:47Z)] TableOfContents
1. Drag and Drop in PyQt4
"" In this part of the PyQt4 tutorial, we will talk about drag & drop operations.""
这部分PyQt4教程我们将了解拖放操作。
In computer graphical user interfaces, drag-and-drop is the action of (or support for the action of) clicking on a virtual object and dragging it to a different location or onto another virtual object. In general, it can be used to invoke many kinds of actions, or create various types of associations between two abstract objects. (Wikipedia)
在计算机图形用户界面中,拖放操作是指点击一个虚拟对象并把它拖到一个不同的位置或其他的虚拟对象上的操作(或支持的行为).一般来说,可以调用多种操作,或创建两个抽象对象间的特定类型的关联。(Wikipedia)
Drag and drop functionality is one of the most visible aspects of the graphical user interface. Drag and drop operation enables users to do complex things intuitively.
- 拖放功能是最明显的图形用户接口外观,拖放操作允许用户直观的组合事物。
Usually, we can drag and drop two things. Data or some graphical objects. If we drag an image from one application to another, we drag and drop binary data. If we drag a tab in Firefox and move it to another place, we drag and drop a graphical component.
- 一般来说,我们可以拖放两件事情,数据或依稀图形对象。如果我们将一幅图像从一个应用程序拖到另一个,我们拖放的是二进制数据。如果我们拖起一个火狐的标签并将它放到另一个地方,我们拖放的是图形组件。
1.1. Simple Drag and Drop
简单的拖放
- In the first example, we will have a QLineEdit and a QPushButton. We will drag plain text from the line edit widget and drop it onto the button widget.
第一个例子中,我们有一个QLineEdit和一个QPushButton。我们将从行编辑组件中拖动无格式的文本并将它放到按钮组件上。
1 #!/usr/bin/python
2 # dragdrop.py
3 import sys
4 from PyQt4 import QtGui
5 class Button(QtGui.QPushButton):
6 def __init__(self, title, parent):
7 QtGui.QPushButton.__init__(self, title, parent)
8 self.setAcceptDrops(True)
9 def dragEnterEvent(self, event):
10 if event.mimeData().hasFormat('text/plain'):
11 event.accept()
12 else:
13 event.ignore()
14 def dropEvent(self, event):
15 self.setText(event.mimeData().text())
16 class DragDrop(QtGui.QDialog):
17 def __init__(self, parent=None):
18 QtGui.QDialog.__init__(self, parent)
19 self.resize(280, 150)
20 self.setWindowTitle('Simple Drag & Drop')
21 edit = QtGui.QLineEdit('', self)
22 edit.setDragEnabled(True)
23 edit.move(30, 65)
24 button = Button("Button", self)
25 button.move(170, 65)
26
27
28 screen = QtGui.QDesktopWidget().screenGeometry()
29 size = self.geometry()
30 self.move((screen.width()-size.width())/2,
31 (screen.height()-size.height())/2)
32 app = QtGui.QApplication(sys.argv)
33 icon = DragDrop()
34 icon.show()
35 app.exec_()
- In order to drop text on the QPushButton widget, we must reimplement some methods. So we create our own Button class, which will inherit from the QPushButton widget.
为了将文本放到QPushButton组件,我们必须重载一些方法,所以我们通过继承QPushButton组件来创建我们自己的按钮类。
self.setAcceptDrops(True)
- We enable drop events for the QPushButton widget.
我们允许QPushButton的放置事件。
- First we reimplement the dragEnteEvent() method. We inform about the data type, we will accept. In our case it is plain text.
首先我们重载dragEnteEvent()方法,我们通知我们将要收到的数据类型,这里是无格式文本。
- By reimplementing the dropEvent() method, we will define, what we will do upon the drop event. Here we change the text of the button widget.
通过重载dropEvent()方法,我们定义我们收到drop事件后如何操作,这里我们改变按钮组件显示的文本。
edit = QtGui.QLineEdit('', self) edit.setDragEnabled(True)
- The QLineEdit widget has a built-in support for drag operations. All we need to do is to call setDragEnabled() method to activate it.
QLineEdit组件有内置的拖动操作,我们所要作的就是调用setDragEnabled()方法来接收它。
Simple Drag & Drop Figure: Simple Drag & Drop
1.2. Drag & drop a button widget
拖放按钮组件
In the following example, we will demonstrate, how to drag & drop a button widget.
以下的例子中我们将示范如何拖放一个按钮组件。
1 #!/usr/bin/python
2 # dragbutton.py
3 import sys
4 from PyQt4 import QtGui
5 from PyQt4 import QtCore
6 class Button(QtGui.QPushButton):
7 def __init__(self, title, parent):
8 QtGui.QPushButton.__init__(self, title, parent)
9 def mouseMoveEvent(self, event):
10
11 if event.buttons() != QtCore.Qt.RightButton:
12 return
13 mimeData = QtCore.QMimeData()
14 drag = QtGui.QDrag(self)
15 drag.setMimeData(mimeData)
16 drag.setHotSpot(event.pos() - self.rect().topLeft())
17 dropAction = drag.start(QtCore.Qt.MoveAction)
18 if dropAction == QtCore.Qt.MoveAction:
19 self.close()
20 def mousePressEvent(self, event):
21 QtGui.QPushButton.mousePressEvent(self, event)
22 if event.button() == QtCore.Qt.LeftButton:
23 print 'press'
24 class DragButton(QtGui.QDialog):
25 def __init__(self, parent=None):
26 QtGui.QDialog.__init__(self, parent)
27 self.resize(280, 150)
28 self.setWindowTitle('Click or Move')
29 self.setAcceptDrops(True)
30 self.button = Button('Close', self)
31 self.button.move(100, 65)
32
33
34 screen = QtGui.QDesktopWidget().screenGeometry()
35 size = self.geometry()
36 self.move((screen.width()-size.width())/2,
37 (screen.height()-size.height())/2)
38 def dragEnterEvent(self, event):
39 event.accept()
40 def dropEvent(self, event):
41 position = event.pos()
42 button = Button('Close', self)
43 button.move(position)
44 button.show()
45 event.setDropAction(QtCore.Qt.MoveAction)
46 event.accept()
47 app = QtGui.QApplication(sys.argv)
48 db = DragButton()
49 db.show()
50 app.exec_()
In our code example, we have a QPushButton on the window. If we click on the button with a left mouse button, we print 'press' to the console. By right clicking and moving the button, we perform a drag & drop operation on the button widget.
在我们的例子里,窗口有一个QPushButton,如果我们鼠标左键点击按钮,我们在终端上打印'press',如果我们右键点击并拖动按钮,我们对按钮组件执行拖放操作。
We create a Button class, which will derive from the QPushButton. We also reimplement two methods of the QPushButton. mouseMoveEvent() and mousePressEvent(). The mouseMoveEvent() method is the place, where the drag & drop operation begins.
我们创建一个起源自QPushButton的按钮类。我们同时重载QPushButton的两个方法mouseMoveEvent()和mousePressEvent()。mouseMoveEvent()方法是拖放操作开始的位置。
if event.buttons() != QtCore.Qt.RightButton: return
Here we decide, that we can perform drag & drop only with a right mouse button. The left mouse button is reserved for clicking on the button.
这里我们设定只对鼠标右键的拖放操作作出响应,对鼠标左键操作保留给点击按钮。
mimeData = QtCore.QMimeData() drag = QtGui.QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(event.pos() - self.rect().topLeft())
- Here we create a QDrag object.
这里我们创建一个QDrag对象。
dropAction = drag.start(QtCore.Qt.MoveAction) if dropAction == QtCore.Qt.MoveAction: self.close()
The start() method of the drag object starts the drag & drop operation. If we perform a move drop action, we destory the button widget. Technically, we destroy a widget on the current position and recreate it on a new position.
拖动对象的start()方法开始拖放操作。如果我们完成一次移动放置操作,我们要销毁按钮组件。技术上来讲,我们在当前位置销毁一个组件,并在新位置重新创建它。
- We print 'press' to the console, if we left click on the button with the mouse. Notice that we call mousePressEvent() method on the parent as well. Otherwise we would not see the button being pushed.
如果我们点击鼠标左键,就在控制台打印'press'。注意,这里我们调用了父类的mousePressEvent()方法,否则我们无法看到鼠标被按下的效果。
position = event.pos() button = Button('Close', self) button.move(position) button.show()
- In the dropEvent() method we code, what happens after we release the mouse button and finish the drop operation. In our example, we create a new Button widget at the current position of the mouse pointer.
在dropEvent()方法中包含了当我们释放鼠标按键并且结束放置后的操作的代码。在我们的例子里,我们在鼠标指针的当前位置创建了一个新的按钮组件。
event.setDropAction(QtCore.Qt.MoveAction) event.accept()
- We specify the type of the drop action. In our case it is a move action.
我们指定释放操作的类型。这里是移动操作。