Simple drag and drop
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.
Simple drag and drop
In the first example, we have a QtGui.QLineEdit and 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.QPushButton widget, we must reimplement some methods. Therefore, we create our own Button class which inherits from the QtGui.QPushButton class.
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.QLineEdit widget has a built-in support for drag operations. All we need to do is to callsetDragEnabled() method to activate it.
Figure: Simple drag & drop
Simple drag and drop的更多相关文章
- [Javascript + rxjs] Simple drag and drop with Observables
Armed with the map and concatAll functions, we can create fairly complex interactions in a simple wa ...
- ZetCode PyQt4 tutorial Drag and Drop
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple ...
- 20 Best Drag and Drop jQuery Plugins--reference
reference from:http://dizyne.net/20-best-drag-drop-jquery-plugins/ jQuery has done a great job repla ...
- angularjs drag and drop
angular-dragula Drag and drop so simple it hurts 480 live demo angular-drag-and-drop-lists Angular d ...
- 『HTML5梦幻之旅』 - 仿Qt演示样例Drag and Drop Robot(换装机器人)
起源 在Qt的演示样例中看到了一个有趣的demo.截图例如以下: 这个demo的名字叫Drag and Drop Robot,简单概括而言,在这个demo中,能够把机器人四周的颜色拖动到机器人的各个部 ...
- HTML5 之拖放(drag与drop)
拖放(Drag 和 drop)是 HTML5 标准的组成部分. 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. HTML5 拖放实例 ...
- 通过HTML5的Drag and Drop生成拓扑图片Base64信息
HTML5 原生的 Drag and Drop是很不错的功能,网上使用例子较多如 http://html5demos.com/drag ,但这些例子大部分没实际用途,本文将搞个有点使用价值的例子,通过 ...
- 基于HTML5的Drag and Drop生成图片Base64信息
HTML5的Drag and Drop是很不错的功能,网上使用例子较多如 http://html5demos.com/drag ,但这些例子大部分没实际用途,本文将搞个有点使用价值的例子,通过Drag ...
- Android 用户界面---拖放(Drag and Drop)(三)
设计拖放操作 本节主要内容如下: 1. 如何开始拖拽: 2. 在拖拽期间如何响应事件: 3. 如何响应落下事件: 4. 如何结束拖放操作. 开始拖拽 用户使用一个拖拽手势开始拖拽,通常是在 ...
随机推荐
- Properties Editor 中文编辑器 汉化
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 在Eclipse的 [Help]-> [ Install New Software ...
- 安卓 listView 优化
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_m ...
- [HDU3240]Counting Binary Trees(不互质同余除法)
Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Failed to read auto-increment value from storage engine错误的处理方法
在进行数据的插入时,系统提示Failed to read auto-increment value from storage engine(从存储引擎读取自增字段失败)错误,经查阅资料,解决方法如下: ...
- 【PHP内存泄漏案例】PHP对象递归引用造成内存泄漏
[案例一] 作者:老王 如果PHP对象存在递归引用,就会出现内存泄漏.这个Bug在PHP里已经存在很久很久了,先让我们来重现这个Bug,代码如下: <?php class Foo { funct ...
- Visual Studio 2013在线文档地址备案
Visual Studio 2013还没有离线的帮助文档,这里做个记录,以备后面使用. 地址:http://msdn.microsoft.com/zh-cn/library/dd831853.aspx
- SharePoint Server 2013 Offline Installation (without Internet)
转自:http://social.msdn.microsoft.com/Forums/sharepoint/zh-CN/08f90e0f-1f52-4eba-9f6e-4dd635ffaadc/sha ...
- unity基础开发----unity游戏速度更快的简易检查表
让游戏速度更快的简易检查表 保持顶点数在 200K 下面,针对 PC 时每帧应为 3M,主要取决于目标 GPU. 若使用内置着色器,请在移动 (Mobile) 或未点亮 (Unlit) 的类别中选择. ...
- Fiddler2 抓取手机APP数据包
原文:http://blog.goyiyo.com/archives/2044 下载安装运行后,查出运行机器的IP,手机连接同一网域内的WIFI,手机WIFI连接设置里的高级里,代理设置填写上Fidd ...
- unity 3D + Google Play In-app Billing (IAB)(转) 热度 3
最近由于工作需要,研究unity如何接入Google Play以实现游戏内购买.目前IAB的实现,prime31做的插件比较好,各平台的IAB均有,但费用相对过高(几乎都是70刀左右,可怜穷小子).在 ...