mirror of
https://github.com/maicss/PyQt5-Chinese-tutorial.git
synced 2025-01-24 21:12:54 +08:00
Update 拖拽.md
This commit is contained in:
parent
1dec9608b4
commit
bcdefc4212
50
拖拽.md
50
拖拽.md
@ -1,17 +1,17 @@
|
||||
# 拖拽
|
||||
|
||||
In this part of the PyQt5 tutorial, we will talk about drag & drop operations.
|
||||
本章讲述的是拖放操作
|
||||
|
||||
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.
|
||||
在GUI里,拖放是指用户点击一个虚拟的对象,拖动,然后放置到另外一个对象上面的动作。一般情况下,需要调用很多动作和方法,创建很多变量。
|
||||
|
||||
Drag and drop is part of the graphical user interface. Drag and drop operations enable 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.
|
||||
一般情况下,我们可以拖放两种东西:数据和图形界面。把一个图像从一个应用拖放到另外一个应用上的实质是操作二进制数据。吧一个表格从Firefox上拖放到另外一个位置 的实质是操作一个图形组。
|
||||
|
||||
Simple drag and drop
|
||||
|
||||
In the first example, we have a QLineEdit and a QPushButton. We drag plain text from the line edit widget and drop it onto the button widget. The button's label will change.
|
||||
## 简单的拖放
|
||||
|
||||
本例使用了`QLineEdit`和`QPushButton`。把一个文本从编辑框里拖到按钮上,更新按钮上的标签(文字)。
|
||||
```
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@ -76,40 +76,48 @@ if __name__ == '__main__':
|
||||
app = QApplication(sys.argv)
|
||||
ex = Example()
|
||||
ex.show()
|
||||
app.exec_()
|
||||
The example presents a simple drag & drop operation.
|
||||
app.exec_()
|
||||
```
|
||||
|
||||
```
|
||||
class Button(QPushButton):
|
||||
|
||||
def __init__(self, title, parent):
|
||||
super().__init__(title, parent)
|
||||
|
||||
self.setAcceptDrops(True)
|
||||
In order to drop text on the QPushButton widget, we must reimplement some methods. Therefore, we create our own Button class which will inherit from the QPushButton class.
|
||||
|
||||
```
|
||||
为了完成预定目标,我们要重构一些方法。首先用`QPushButton`上构造一个按钮实例。
|
||||
```
|
||||
self.setAcceptDrops(True)
|
||||
We enable drop events for the widget.
|
||||
|
||||
```
|
||||
激活组件的拖拽事件。
|
||||
```
|
||||
def dragEnterEvent(self, e):
|
||||
|
||||
if e.mimeData().hasFormat('text/plain'):
|
||||
e.accept()
|
||||
else:
|
||||
e.ignore()
|
||||
First, we reimplement the dragEnterEvent() method. We inform about the data type that we accept. In our case it is plain text.
|
||||
|
||||
```
|
||||
首先,我们重构了`dragEnterEvent()`方法。设定好接受拖拽的数据类型(plain text)。
|
||||
```
|
||||
def dropEvent(self, e):
|
||||
|
||||
self.setText(e.mimeData().text())
|
||||
By reimplementing the dropEvent() method we define what we will do upon the drop event. Here we change the text of the button widget.
|
||||
|
||||
```
|
||||
然后重构`dropEvent()`方法,更改按钮接受鼠标的释放事件的默认行为。
|
||||
```
|
||||
edit = QLineEdit('', self)
|
||||
edit.setDragEnabled(True)
|
||||
The QLineEdit widget has a built-in support for drag operations. All we need to do is to call the setDragEnabled() method to activate it.
|
||||
```
|
||||
`QLineEdit`默认支持拖拽操作,所以我们只要调用`setDragEnabled()`方法使用就行了。
|
||||
|
||||
Simple drag & drop
|
||||
Figure: Simple drag & drop
|
||||
Drag & drop a button widget
|
||||
程序展示:
|
||||
|
||||
![drag & drop](./images/8-dragdrop.png)
|
||||
|
||||
## Drag & drop a button widget
|
||||
|
||||
In the following example, we will demonstrate how to drag & drop a button widget.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user