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, ...
随机推荐
- R语言apply()函数用法
在R语言的帮助文档里,apply函数的功能是: Retruns a vector or array or list of values obtained by applying a function ...
- Missing Number-[回溯][难]
2. Missing number 转自:https://mp.weixin.qq.com/s/WLRXLdi-3igkjtiWlHg7Ug Given a positive integer n(n≤ ...
- python16_day35【算法】
一.BTree class BinTreeNode: def __init__(self, data): self.data = data self.lchild = None self.rchild ...
- 时间格式—My97DatePicker控件使用
一.My97DatePicker控件使用(2步): 1:JSP页面:( class="Wdate" 显示控件图标) 引入控件包中的脚本: <script type=&qu ...
- 【android】开发笔记系列:行为篇
1:键盘遮挡了输入框 在androidManifest.xml里,对应的activity里设置键盘模式 <activity android:name="活动名称" andro ...
- Python: 字符串格式化format()函数的使用
python从2.6开始支持format,新的更加容易读懂的字符串格式化方法,从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序号, ...
- impress.js初体验——前端装X利器
impress.js 是国外一位开发者受 Prezi 启发,采用 CSS3 与 JavaScript 语言完成的一个可供开发者使用的表现层框架(演示工具).其功能包括画布的无限旋转与缩放,任意角度放置 ...
- 网络安全、Web安全、渗透测试之笔经面经总结(一)
本篇文章总结涉及以下几个方面: 对称加密非对称加密? 什么是同源策略? cookie存在哪里?可以打开吗 xss如何盗取cookie? tcp.udp的区别及tcp三次握手,syn攻击? 证书要考哪些 ...
- 比较两个JSON字符串是否完全相等
RT,比较两个JSON字符串是否完全相等,这里使用google贡献的Gson. 一,no POJO,即不另外创建一个简单Java类 [java] view plain copy String str1 ...
- 发起图片请求的几种可能性(webkit内核)
网页测试源代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...