Advertisement
Guest User

PySide: DragNDrop, copy widgets

a guest
Apr 13th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. from PySide import QtGui, QtCore
  6.  
  7.  
  8. class Button(QtGui.QPushButton):
  9.  
  10.     def __init__(self, title, parent):
  11.         super(Button, self).__init__(title, parent)
  12.  
  13.     def mouseMoveEvent(self, e):
  14.  
  15.         if e.buttons() == QtCore.Qt.LeftButton:
  16.             self.setDown(False)
  17.             mimeData = QtCore.QMimeData()
  18.    
  19.             drag = QtGui.QDrag(self)
  20.             drag.setMimeData(mimeData)
  21.             drag.setHotSpot(e.pos() - self.rect().topLeft())
  22.    
  23.             drag.start(QtCore.Qt.MoveAction)
  24.  
  25.  
  26.     def mousePressEvent(self, e):
  27.      
  28.         QtGui.QPushButton.mousePressEvent(self, e)
  29.         if e.button() == QtCore.Qt.LeftButton:
  30.             print 'press'
  31.        
  32.  
  33. class Example(QtGui.QWidget):
  34.    
  35.     def __init__(self):
  36.         super(Example, self).__init__()
  37.        
  38.         self.initUI()
  39.        
  40.     def initUI(self):      
  41.    
  42.         self.setAcceptDrops(True)
  43.  
  44.         self.btn = Button('Button', self)
  45.         self.btn.move(100, 65)
  46.  
  47.         self.setGeometry(300, 300, 300, 150)
  48.         self.setWindowTitle('Click or move')
  49.         self.show()
  50.  
  51.     def dragEnterEvent(self, e):
  52.      
  53.         e.accept()
  54.  
  55.     def dropEvent(self, e):
  56.         position = e.pos()
  57.         newButton = Button(self.btn.text(), self)
  58.         newButton.show()
  59.         newButton.move(position)
  60.  
  61.         e.setDropAction(QtCore.Qt.MoveAction)
  62.         e.accept()
  63.         print 'Original button is down:', self.btn.isDown()
  64.         print 'New button is down:', newButton.isDown()
  65.        
  66. def main():
  67.    
  68.     app = QtGui.QApplication(sys.argv)
  69.     ex = Example()
  70.     sys.exit(app.exec_())
  71.  
  72.  
  73. if __name__ == '__main__':
  74.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement