Drag & drop a button widget
In the following example, we will demonstrate how to drag & drop a button widget.
#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this program, we can press on a button
with a left mouse click or drag and drop the
button with the right mouse click. author: Jan Bodnar
website: zetcode.com
last edited: October 2013
""" import sys
from PyQt4 import QtCore, QtGui class Button(QtGui.QPushButton): def __init__(self, title, parent):
super(Button, self).__init__(title, parent) def mouseMoveEvent(self, e): if e.buttons() != QtCore.Qt.RightButton:
return mimeData = QtCore.QMimeData() drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft()) dropAction = drag.start(QtCore.Qt.MoveAction) def mousePressEvent(self, e): super(Button, self).mousePressEvent(e) if e.button() == QtCore.Qt.LeftButton:
print 'press' class Example(QtGui.QWidget): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): self.setAcceptDrops(True) self.button = Button('Button', self)
self.button.move(100, 65) self.setWindowTitle('Click or Move')
self.setGeometry(300, 300, 280, 150)
self.show() def dragEnterEvent(self, e): e.accept() def dropEvent(self, e): position = e.pos()
self.button.move(position) e.setDropAction(QtCore.Qt.MoveAction)
e.accept() def main(): app = QtGui.QApplication([])
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()
In our code example, we have a QtGui.QPushButton on the window. If we click on the button with a left mouse button, the 'press' message is printed to the console. By right clicking and moving the button, we perform a drag & drop operation on the button widget.
class Button(QtGui.QPushButton):
def __init__(self, title, parent):
super(Button, self).__init__(title, parent)
We create a Button class which will derive from the QtGui.QPushButton. We also reimplement two methods of the QtGui.QPushButton: the mouseMoveEvent() and the mousePressEvent(). ThemouseMoveEvent() method is the place where the drag & drop operation begins.
if event.buttons() != QtCore.Qt.RightButton:
return
Here we decide that we can perform drag & drop only with a right mouse button. The left mouse button is reserved for clicking on the button.
mimeData = QtCore.QMimeData() drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(event.pos() - self.rect().topLeft())
The QDrag object is created. The class provides support for MIME-based drag and drop data transfer.
dropAction = drag.start(QtCore.Qt.MoveAction)
The start() method of the drag object starts the drag & drop operation.
def mousePressEvent(self, e):
super(Button, self).mousePressEvent(e)
if e.button() == QtCore.Qt.LeftButton:
print 'press'
We print 'press' to the console if we left click on the button with the mouse. Notice that we callmousePressEvent() method on the parent as well. Otherwise, we would not see the button being pushed.
position = e.pos()
self.button.move(position)
In the dropEvent() method we code what happens after we release the mouse button and finish the drop operation. We find out the current mouse pointer position and move the button accordingly.
e.setDropAction(QtCore.Qt.MoveAction)
e.accept()
We specify the type of the drop action. In our case it is a move action.
Drag & drop a button widget的更多相关文章
- 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop
[源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...
- HTML 学习笔记 (drag & drop)
拖放(Drag & Drop)是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放.过去,我们用监听鼠标的Mousedown.Mouseo ...
- HTML5魔法堂:全面理解Drag & Drop API
一.前言 在HTML4的时代,各前端工程师为了实现拖拽功能可说是煞费苦心,初听HTML5的DnD API觉得那些痛苦的日子将一去不复返,但事实又是怎样的呢?下面我们一起来看看DnD API的真面 ...
- Win10/UWP新特性—Drag&Drop 拖出元素到其他App
在以前的文章中,写过微软新特性Drag&Drop,当时可能由于处于Win10预览版,使用的VS也是预览版,只实现了从桌面拖拽文件到UWP App中,没能实现从UWP拖拽元素到Desktop A ...
- Draggabilly – 轻松实现拖放功能(Drag & Drop)
Draggabilly 是一个很小的 JavaScript 库,专注于拖放功能.只需要简单的设置参数就可以在你的网站用添加拖放功能.兼容 IE8+ 浏览器,支持多点触摸.可以灵活绑定事件,支持 Req ...
- JS魔法堂:IE5~9的Drag&Drop API
一.前言 < HTML5魔法堂:全面理解Drag & Drop API>中提到从IE5开始已经支持DnD API,但IE5~9与HTML5的API有所不同,下面我们来了解一 ...
- [转]人人网首页拖拽上传详解(HTML5 Drag&Drop、FileReader API、formdata)
人人网首页拖拽上传详解(HTML5 Drag&Drop.FileReader API.formdata) 2011年12月11日 | 彬Go 上一篇:给力的 Google HTML5 训练营( ...
- Drag & Drop and File Reader
参考 : http://www.html5rocks.com/zh/tutorials/file/dndfiles/ http://blog.csdn.net/rnzuozuo/article/det ...
- Android drag drop
最近偶尔知道了锤子的one step,所以在网上看相关的东西,有人说android原生drag drop就能实现,我就去学习一下这个drag drop,下面把学习到的东西总结一下: drag drop ...
随机推荐
- noip2012疫情控制 题解
题目大意 给出一棵n个节点的树,根是1,要在除根节点以外的点建立检查点,使得从每条根到叶子的路径上都至少存在一个检查点.检查点由军队来建立.初始军队的位置是给定的,移动军队走一条边需要花费这条边的权值 ...
- 「TJOI 2018」游园会 Party
「TJOI 2018」游园会 Party 题目描述 小豆参加了 \(NOI\) 的游园会,会场上每完成一个项目就会获得一个奖章,奖章只会是 \(N, O, I\) 的字样. 在会场上他收集到了 \(K ...
- [BZOJ4560][JLOI2016]字符串覆盖(贪心+DP)
先用KMP求出所有可以放的位置,然后两个值分别处理. 最大值: 贪心,4!枚举放的先后位置顺序,2^3枚举相邻两个串是否有交. 若有交,则后一个的起始位置一定是离前一个的结束位置最近的位置,无交也一样 ...
- 【洛谷】P1176: 路径计数2【递推】
P1176 路径计数2 题目描述 一个N×N的网格,你一开始在(1,1),即左上角.每次只能移动到下方相邻的格子或者右方相邻的格子,问到达(N,N),即右下角有多少种方法. 但是这个问题太简单了,所以 ...
- hdu4337 King Arthur's Knights
King Arthur's Knights Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Ext.form.ComboBox常用属性详解
Ext.form.ComboBox常用属性详解 标签: Extjs js combo js 代码 var combo = new Ext.form.ComboBox({ store : new Ext ...
- 在雇员表中查找第二高的工资SQL语句助记
"在雇员表中查找第二高的工资的员工记录"SQL语句怎么写 这个查询首先查找最高工资,然后将它从列表中排除.再查找最高工资. 非常明显,第二次返回的是 ...
- 降压转换器 (Buck)
降压转换器 (Buck) 切换式降压转换器 (Buck) 能提供高效率.高度弹性.高压降比.高负载能力的降压转换. 多数降压转换器 (Buck) 包含上桥 MOSFET 和同步整流 MOSFET,根据 ...
- svn 冲突
转载:http://blog.sina.com.cn/s/blog_65fd4c1e0100h2cg.html 1. 如何产生冲突 当开发人员A和开发人员B从版本库同时检出文档1.txt,而A和B同 ...
- easyui 设置一加载,搜索框立即弹出的效果
1.部分html文件 <div id="searchForm" region="north" title="标的查询" collaps ...