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. 如何结束拖放操作. 开始拖拽 用户使用一个拖拽手势开始拖拽,通常是在 ...
随机推荐
- 安卓 listView 优化
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_m ...
- [NOI2015]寿司晚宴 --- 状压DP
[NOI2015]寿司晚宴 题目描述 为了庆祝NOI的成功开幕,主办方为大家准备了一场寿司晚宴. 小G和小W作为参加NOI的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了n−1种不同的寿 ...
- [ZJOI2007]最大半连通子图
[ZJOI2007]最大半连通子图 题目大意: 一个有向图称为半连通的,当且仅当对于任意两点\(u,v\),都满足\(u\)能到达\(v\)或者\(v\)能到达\(u\). 给定一个\(n(n\le1 ...
- vagrant 常用命令以及常用操作
列出这些命令,主要是防止脑内存不足.目前这些命令是我常用的,以后其他命令用的多,我再继续添加... 分享些本人用的百度网盘box,国外的太坑... 本人分享的百度网盘:http://pan.baidu ...
- WebSQL的基本使用过程
1.创建或打开数据库(openDatabase) var db = openDatabase('dbname', '1.0', 'discription', 2 * 1024); // 目前测试只有C ...
- Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present 记忆化搜索
D. Mysterious Present 题目连接: http://www.codeforces.com/contest/4/problem/D Description Peter decided ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- 【原】【Git】EGit强制覆盖本地文件
今天带来的是EGit使用中有时会用到的一个技巧,强制覆盖本地.EGit强制覆盖远端(其实没有这个说法),在另一篇文章中写了http://www.cnblogs.com/guodongdidi/p/48 ...
- What is the Linux High Availabi
What is the Linux High Availabi 简介: 高可用性群集的出现是为了使群集的整体服务尽可能可用,以便考虑计算硬件和软件的易错性.如果高可用性群集中的主节点发生 ...
- C# http Get/POST请求封装类
C#HttpHelper官方产品发布与源码下载---苏飞版 http://www.sufeinet.com/thread-3-1-1.html 在C#用HttpWebRequest中发送GET/HTT ...