#!/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 # 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.
class Button(QtGui.QPushButton): def __init__(self, title, parent):
super(Button, self).__init__(title, parent) # We enable drop events for the widget.
self.setAcceptDrops(True) # First, we reimplement the dragEnterEvent() method. We inform about the data type that we accept. In our case it is plain text.
def dragEnterEvent(self, e): if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore() # By reimplementing the dropEvent() method we define what we will do upon the drop event. Here we change the text of the button widget.
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)
# The QtGui.QLineEdit widget has a built-in support for drag operations. All we need to do is to call setDragEnabled() method to activate it.
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() -------------------------------------------------------------------------------- #!/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 # 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(). The mouseMoveEvent() method is the place where the drag & drop operation begins.
class Button(QtGui.QPushButton): def __init__(self, title, parent):
super(Button, self).__init__(title, parent) def mouseMoveEvent(self, e): # 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.
if e.buttons() != QtCore.Qt.RightButton:
return # The QDrag object is created. The class provides support for MIME-based drag and drop data transfer.
mimeData = QtCore.QMimeData() drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft()) # The start() method of the drag object starts the drag & drop operation.
dropAction = drag.start(QtCore.Qt.MoveAction) # We print 'press' to the console if we left click on the button with the mouse. Notice that we call mousePressEvent() method on the parent as well. Otherwise, we would not see the button being pushed.
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): # 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.
position = e.pos()
self.button.move(position) # We specify the type of the drop action. In our case it is a move action.
e.setDropAction(QtCore.Qt.MoveAction)
e.accept() def main(): app = QtGui.QApplication([])
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

ZetCode PyQt4 tutorial Drag and Drop的更多相关文章

  1. ZetCode PyQt4 tutorial widgets II

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  2. ZetCode PyQt4 tutorial widgets I

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  3. ZetCode PyQt4 tutorial Dialogs

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  4. ZetCode PyQt4 tutorial signals and slots

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  5. ZetCode PyQt4 tutorial layout management

    !/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows ...

  6. ZetCode PyQt4 tutorial work with menus, toolbars, a statusbar, and a main application window

    !/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This program create ...

  7. ZetCode PyQt4 tutorial First programs

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  8. ZetCode PyQt4 tutorial custom widget

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  9. ZetCode PyQt4 tutorial basic painting

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

随机推荐

  1. 自定义centos7 yum仓库

    将安装光盘插入 mkdir /newyum umount /dev/sr0 mount /dev/sr0 /media cp -rf /media/Packages /newyum #将镜像中的rpm ...

  2. CCPC-Wannafly Winter Camp Day2 (Div2, onsite)

    Class $A_i = a \cdot i \% n$ 有 $A_i = k \cdot gcd(a, n)$ 证明: $A_0 = 0, A_x = x \cdot a - y \cdot n$ ...

  3. ng-深度学习-课程笔记-13: 目标检测(Week3)

    1 目标定位( object localization ) 目标定位既要识别,又要定位,它要做的事就是用一个框框把物体目标的位置标出来. 怎么做这个问题呢,我们考虑三目标的定位问题,假定图中最多只出现 ...

  4. 简单了解下CGI、FastCGI和php-fpm的概念和区别和运行原理

    什么是CGI? CGI(Common Gateway Interface),公共网关接口,它是Web服务器与外部应用程序(CGI程序)之间传递信息的接口标准.通过CGI接口,Web服务器就能够获取客户 ...

  5. 2018-2019-1 20189215《Linux内核原理与分析》第三周作业

    <庖丁解牛>第二章书本知识总结 函数调用框架 call指令有两个作用: (1) 将CS:EIP中下一条指令的地址A保存在栈顶: (2)设置CS:EIP指向被调用程序的第一行. ret指令在 ...

  6. c++生成算式并计算(《构建之法》第一章课后第一题)

    c++实现计算器(自动生成算式并计算) 要满足的需求有以下几个: 自动生成随机的四则运算算式,包含括号和小数. 对生成的算式计算出结果. 算式.结果分别存储到不同的文件. 一 生成算式 由上述需求可知 ...

  7. RHEL6.5恢复root密码

    1.开机上下键停留在如下界面,键盘输入小写e: 2.选择如下选项,并输入小写e: 3.输入1,回车进入单用户模式: 4.键盘输入小写b,进行启动: 5.进入到单用户模式: 6.修改root用户密码,并 ...

  8. Android反射机制:手把手教你实现反射

    什么是反射机制? JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称 ...

  9. 配置github的SSH key及GitHub项目上传方式一——使用终端命令行

    GitHub是一个开源的大仓库,我们经常从github上下载项目进行学习和研究,下面是一个完整的步骤——往GitHub上传一个新项目. 一.注册GitHub账号 1.注册GitHub账号,地址:htt ...

  10. 快捷方式控制台调试each这种方法的时候怎么停

    1.ctrl +' 2.当遇到angular.each的时候ctrl + ; 3.进入之后,还是先ctrl+'; angular中: 当遇到forEach之后,又一次ctrl+;就回到你的each之后 ...