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的更多相关文章

  1. [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 ...

  2. ZetCode PyQt4 tutorial Drag and Drop

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple ...

  3. 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 ...

  4. angularjs drag and drop

    angular-dragula Drag and drop so simple it hurts 480 live demo angular-drag-and-drop-lists Angular d ...

  5. 『HTML5梦幻之旅』 - 仿Qt演示样例Drag and Drop Robot(换装机器人)

    起源 在Qt的演示样例中看到了一个有趣的demo.截图例如以下: 这个demo的名字叫Drag and Drop Robot,简单概括而言,在这个demo中,能够把机器人四周的颜色拖动到机器人的各个部 ...

  6. HTML5 之拖放(drag与drop)

    拖放(Drag 和 drop)是 HTML5 标准的组成部分. 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. HTML5 拖放实例 ...

  7. 通过HTML5的Drag and Drop生成拓扑图片Base64信息

    HTML5 原生的 Drag and Drop是很不错的功能,网上使用例子较多如 http://html5demos.com/drag ,但这些例子大部分没实际用途,本文将搞个有点使用价值的例子,通过 ...

  8. 基于HTML5的Drag and Drop生成图片Base64信息

    HTML5的Drag and Drop是很不错的功能,网上使用例子较多如 http://html5demos.com/drag ,但这些例子大部分没实际用途,本文将搞个有点使用价值的例子,通过Drag ...

  9. Android 用户界面---拖放(Drag and Drop)(三)

      设计拖放操作 本节主要内容如下: 1.  如何开始拖拽: 2.  在拖拽期间如何响应事件: 3.  如何响应落下事件: 4.  如何结束拖放操作. 开始拖拽 用户使用一个拖拽手势开始拖拽,通常是在 ...

随机推荐

  1. WebLogic Server

    前几天,看了几集J2ee , 给我的感觉就是,看不懂!! 一点也不懂! 那怎么办呢? 听老师的,不管懂不懂,先看看再说.接下来,就开始了J2ee "艰苦"的历程.在J2ee中,经常 ...

  2. VS2015常用配置

    一.调用控制台: 在VS中使用opencv或者QT过程中,完成编程后, 运行发现没有控制台窗口, 比如我们用Qt编写的界面软件, 又想看到我们在代码中添加的打印日志信息,这个时候加上控制台窗口就能实现 ...

  3. bzoj 1101

    其实这个用的是Mobius反演的第二种形式 F(d) = (n div d) * (m div d) f(d) = [ gcd(i,j)=d ] (i in [1,a], j in [1,b]) /* ...

  4. bzoj 3784

    第三道点分治. 首先找到黄学长的题解,他叫我参考XXX的题解,但已经没有了,然后找到另一个博客的简略题解,没看懂,最后看了一个晚上黄学长代码,写出来然后,写暴力都拍了小数据,但居然超时,....然后改 ...

  5. Miller-Rabin算法 codevs 1702 素数判定 2

    转载自:http://www.dxmtb.com/blog/miller-rabbin/ 普通的素数测试我们有O(√ n)的试除算法.事实上,我们有O(slog³n)的算法. 定理一:假如p是质数,且 ...

  6. php -- 解决php连接sqlserver2005中文乱码问题(附详细解决方法)

    @_@~~ --php5.2 --phpstudy --apache --sqlserver2005 @_@~~问题描述 问题一:php连接sqlsever2005,输入中文,然后查询sqlserve ...

  7. linux下如何启动sybase

    isql -Dxxx -Uxxx -P111111 用isql连接数据库发现数据库没有启动. 如何启动sybase数据库? [TA_SYBASE][/home/bta]su - sybase  //切 ...

  8. 初探Delphi中的插件编程

    前言 我写Delphi程序是从MIS系统入门的,开始尝试子系统划分的时候采用的是MDI窗体的结构.随着系统功能的扩充,不断有新的子系统加入系统中,单个工程会变得非常大,每次做一点修改都要重新编译,单个 ...

  9. 关于spring-mvc.xml的mvc:resources元素浅析。

    配置如下: <!-- 配置静态资源 --><mvc:resources location="/static/" mapping="/static/**& ...

  10. JavaScript中的call和apply应用

    ECMAScript3给Function的原型定义了两个方法,他们是Function.prototype.call 和 Function.prototype.apply. 在实际开发中,特别是在一些函 ...