Simple drag and drop
2015-04-18 10:23
211 查看
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.
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.
The example presents a simple drag & drop operation.
In order to drop text on the
We enable drop events for the widget.
First, we reimplement the
By reimplementing the
The
Figure: Simple drag & drop
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.
Simple drag and drop
In the first example, we have aQtGui.QLineEditand a
QtGui.QPushButton. We drag plain text from the line edit widget and drop it onto the button widget. The button's label will change.
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple drag and drop example. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt4 import QtGui class Button(QtGui.QPushButton): def __init__(self, title, parent): super(Button, self).__init__(title, parent) self.setAcceptDrops(True) def dragEnterEvent(self, e): if e.mimeData().hasFormat('text/plain'): e.accept() else: e.ignore() def dropEvent(self, e): self.setText(e.mimeData().text()) class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): edit = QtGui.QLineEdit('', self) edit.setDragEnabled(True) edit.move(30, 65) button = Button("Button", self) button.move(190, 65) self.setWindowTitle('Simple drag & drop') self.setGeometry(300, 300, 300, 150) def main(): app = QtGui.QApplication(sys.argv) ex = Example() ex.show() app.exec_() if __name__ == '__main__': main()
The example presents a simple drag & drop operation.
class Button(QtGui.QPushButton): def __init__(self, title, parent): super(Button, self).__init__(title, parent)
In order to drop text on the
QtGui.QPushButtonwidget, we must reimplement some methods. Therefore, we create our own
Buttonclass which inherits from the
QtGui.QPushButtonclass.
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.
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.
edit = QtGui.QLineEdit('', self) edit.setDragEnabled(True)
The
QtGui.QLineEditwidget has a built-in support for drag operations. All we need to do is to call
setDragEnabled()method to activate it.
Figure: Simple drag & drop
相关文章推荐
- Simple Drag and Drop in ExtJS
- [Javascript + rxjs] Simple drag and drop with Observables
- Very Simple WPF Drag and Drop Sample without Win32 Calls
- HoneyComb3.0技术系列之Drag And Drop(二)
- How To Drag and Drop Sprites with Cocos2D
- XHtmlTree - Tree control with support for HTML, XML, Smart Checkboxes, and Drag & Drop
- Qt Drag and Drop Puzzle Example 看看看~
- UE4蓝图节点文档翻译【目录】--- Drag And Drop
- 基于HTML5的Drag and Drop生成图片Base64信息
- .NET中的Drag and Drop操作(一)
- drag and drop(拖拽)
- Android开发者指南-用户界面-拖放-Drag and Drop[原创译文]
- flex 图片拖放的实现(drag and drop)
- Why Doesn’t Drag-and-Drop work when my Application is Running Elevated? – A: Mandatory Integrity Control and UIPI(转载)
- Drag and Drop
- HTML 5 drag and drop 简介
- VC实现文件管理器拖拽(Drag-and-Drop)
- Drag and Drop For IconJLable
- 学习drag and drop js实现代码经典之作
- HTML5 CSS3 专题 : 拖放 (Drag and Drop)