实在是太多了。。。就此不在写PySide的QtGui模块了,最后一个。--右键可以拖拽按钮的位置
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PySide import QtGui, QtCore
class 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
mime_data = QtCore.QMimeData()
drag = QtGui.QDrag(self)
drag.setMimeData(mime_data)
drag.setHotSpot(e.pos() - self.rect().topLeft())
drag.start(QtCore.Qt.MoveAction)
def mousePressEvent(self, e):
QtGui.QPushButton.mousePressEvent(self, e)
if e.button() == QtCore.Qt.LeftButton:
print 'Left Click'
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.setGeometry(300, 300, 300, 150)
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)
win = Example()
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()