在GUI编程中,拖拽操作即在不同的应用间拖拽对象以实现对象的移动
1、简单的拖拽
为了实现将行编辑器中的文本拖拽到按钮上,我们必须重写一些方法,所以我们在继承QtGui.QPushButton的基础上创造我们自己的BUTTON类
# -*- coding: utf-8 -*- """ ZetCode PySide tutorial This is a simple drag and drop example. author: Jan Bodnar website: zetcode.com last edited: August 2011 """ import sys from PySide import QtGui, QtCore class 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()