ZetCode PyQt4 tutorial Drag and Drop
#!/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的更多相关文章
- ZetCode PyQt4 tutorial widgets II
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial widgets I
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial Dialogs
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial signals and slots
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial layout management
!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows ...
- 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 ...
- ZetCode PyQt4 tutorial First programs
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial custom widget
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial basic painting
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
随机推荐
- PAT 1031 Hello World for U[一般]
1031 Hello World for U (20 分) Given any string of N (≥5) characters, you are asked to form the chara ...
- char *strstr(const char *str1, const char *str2);
[FROM MSDN && 百科] 原型:char *strstr(const char *str1, const char *str2); #include<string.h& ...
- 测试人必备:国内外最好用的6款Bug跟踪管理系统
在移动互联网产品中,Bug会导致软件产品在某种程度上不能满足用户的需要.确保一个项目进展顺利,关键在于妥善处理软件中的BUG,那么,如何高效的管理BUG,解决BUG?在这里,我为大家搜集了几款优秀的B ...
- javascript模式(1)--私有成员
javascript是基于对象的一门语言,没有想java等语言那样子拥有封装的特性.但是javascript可以通过闭包来进行模拟. 1.构造函数与私有成员 可以用构造函数形成一个闭包,实现内部成员的 ...
- 【运维技术】Jenkins配置使用教程
Jenkins配置使用教程 单机jenkins启动 软件安装和启动,必须含有java环境 # 安装jdk,参考其他教程,创建文件目录 mkdir -p /app/jenkins cd /app/jen ...
- @Tranactional事务没有回滚
一.特性 先来了解一下@Transactional注解事务的特性吧,可以更好排查问题 1.service类标签(一般不建议在接口上)上添加@Transactional,可以将整个类纳入spring事务 ...
- 清理tomcat日志大的文件
先看一个命令: [root@weblogic logs]# catalina.--.log icatalina.--:-.out icatalina.--:-.out localhost_access ...
- 想转行学Java,却又担心自己半路出家成不了大牛
想转行学Java,却又担心自己半路出家成不了大牛 很多人看好Java编程的高薪前景,在自己职业生涯迷茫的时候,想转行学Java,却又担心自己半路出家成不了大牛,赚不到钱,本文就为大家分析一下,转行学J ...
- 【javascript】浏览器用户代理检测脚本实现
以下是完整的用户代理字符串检测脚本,包括检测呈现引擎.平台.Windows操作系统.移动设备和游戏系统. var client = function(){ // 呈现引擎 var engine = { ...
- 记第一场atcoder和codeforces 2018-2019 ICPC, NEERC, Northern Eurasia Finals Online Mirror
下午连着两场比赛,爽. 首先是codeforses,我和一位dalao一起打的,结果考炸了,幸亏不计rating.. A Alice the Fan 这个就是记忆化搜索一下预处理,然后直接回答询问好了 ...