PySide学习笔记第十一章-PySide中的拖拽(放)操作
来源:互联网 发布:淘宝客户分析 编辑:程序博客网 时间:2024/05/20 06:54
在GUI编程中,拖拽操作即在不同的应用间拖拽对象以实现对象的移动
1、简单的拖拽
为了实现将行编辑器中的文本拖拽到按钮上,我们必须重写一些方法,所以我们在继承QtGui.QPushButton的基础上创造我们自己的BUTTON类
# -*- coding: utf-8 -*-"""ZetCode PySide tutorialThis is a simple drag anddrop example.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGui, QtCoreclass Button(QtGui.QPushButton): # 定义Button类,以实现拖拽操作 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): qe = QtGui.QLineEdit('', self) qe.setDragEnabled(True) qe.move(30, 65) button = Button("Button", self) button.move(190, 65) self.setGeometry(300, 300, 300, 150) self.setWindowTitle('Simple Drag & Drop') self.show()def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main()二、在下面的示例中,我们会证明 如何拖拽一个button,在本示例中,点击左键会显示 ‘press’,按压住右键可以拖拽按钮
# -*- coding: utf-8 -*-"""ZetCode PySide tutorialIn this example, we drag and drop abutton.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGui, QtCoreclass 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): QtGui.QPushButton.mousePressEvent(self, 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.btn = Button('Button', self) self.btn.move(100, 65) self.setGeometry(300, 300, 300, 150) self.setWindowTitle('Click or move') self.show() def dragEnterEvent(self, e): e.accept() def dropEvent(self, e): position = e.pos() self.btn.move(position) e.setDropAction(QtCore.Qt.MoveAction) e.accept()def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main()
0 0
- PySide学习笔记第十一章-PySide中的拖拽(放)操作
- PySide学习笔记第九章-对话框
- PySide学习笔记第十二章-绘制
- PySide学习笔记第二章-提示框
- PySide学习笔记第四章-消息框
- PySide学习笔记第五章-居中 窗口
- PySide学习笔记第六章-菜单和工具栏
- PySide学习笔记第七章-布局管理
- PySide学习笔记第八章-事件和信号
- pyside
- PySide学习笔记第十章-窗体小部件(widget)-上篇
- PySide学习笔记第十章-窗体小部件(widget)-下篇
- PySide学习笔记第一章-显示应用图标
- PySide学习笔记第三章-利用信号槽机制关闭窗口
- pyside 中的中文字符
- PySide中的信号和插槽
- pyside-环境建立(windows)
- PySide入门
- 打印流PrintWriter实现自动刷新和换行
- python lambda函数基础
- Golang 学习笔记 ---类型和一个 HelloWorld
- 使用打印流复制文件
- Android的回调机制
- PySide学习笔记第十一章-PySide中的拖拽(放)操作
- RecyclerView加载更多数据的三种不同的实现方式
- Part 2 How to Monitor Java Garbage Collection
- Intent实现简易文件浏览查看器
- fork()函数
- http load介绍
- 自定义View android 像支付宝支付界面的progress
- hdu 5414 CRB and String(字符串模拟)
- secureCRT报错 The remote system refused the connection