pyqt darg and drop

Sachin Tiwari sachin.tiwari50 at gmail.com
Sat Oct 4 06:32:35 EDT 2014


On Saturday, October 4, 2014 3:35:33 PM UTC+5:30, Sachin Tiwari wrote:
> Hi
> 
> 
> 
> I want to drag and drop push multiple push buttons but its working for only for last button. And I want to connect them by a wire.
> 
> 
> 
> Please help.
> 
> 
> 
>           import sys
> 
> from PyQt4 import QtGui, QtCore
> 
> 
> 
> 
> 
> class Button(QtGui.QPushButton):
> 
>     def mouseMoveEvent(self, e):
> 
>         if e.buttons() != QtCore.Qt.RightButton:
> 
>             return
> 
> 
> 
>         mimeData = QtCore.QMimeData()
> 
>         mimeData.setText('%d,%d' % (e.x(), e.y()))
> 
> 
> 
>         pixmap = QtGui.QPixmap.grabWidget(self)
> 
> 
> 
>         painter = QtGui.QPainter(pixmap)
> 
>         painter.setCompositionMode(painter.CompositionMode_DestinationIn)
> 
>         painter.fillRect(pixmap.rect(), QtGui.QColor(0, 0, 0, 127))
> 
>         painter.end()
> 
> 
> 
>         drag = QtGui.QDrag(self)
> 
>         drag.setMimeData(mimeData)
> 
>         drag.setPixmap(pixmap)
> 
>         drag.setHotSpot(e.pos())
> 
>         if drag.exec_(QtCore.Qt.CopyAction & QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
> 
>             print 'moved'
> 
>         else:
> 
>             print 'copied'
> 
> 
> 
>     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)
> 
> 
> 
>         button = Button('Button', self)
> 
>         button1 = Button('Button1', self)
> 
>         button.move(100, 65)
> 
>         button1.move(200, 65)
> 
> 
> 
>         self.buttons = [button]
> 
> 
> 
>         self.setWindowTitle('Copy or Move')
> 
>         self.setGeometry(300, 300, 280, 150)
> 
> 
> 
> 
> 
>     def dragEnterEvent(self, e):
> 
>         e.accept()
> 
> 
> 
> 
> 
>     def dropEvent(self, e):
> 
>         mime = e.mimeData().text()
> 
>         x, y = map(int, mime.split(','))
> 
> 
> 
>         if e.keyboardModifiers() & QtCore.Qt.ShiftModifier:
> 
>             button = Button('Button', self)
> 
>             button1 = Button('Button1', self)
> 
>             button.move(e.pos()-QtCore.QPoint(x, y))
> 
>             button1.move(e.pos()-QtCore.QPoint(x, y))
> 
>             button.show()
> 
>             button1.show()
> 
>             self.buttons.append(button)
> 
>             self.buttons.append(button1)
> 
>             e.setDropAction(QtCore.Qt.CopyAction)
> 
>         else:
> 
>             e.source().move(e.pos()-QtCore.QPoint(x, y))
> 
>             e.setDropAction(QtCore.Qt.MoveAction)
> 
>         e.accept()
> 
> 
> 
> 
> 
> if __name__ == '__main__':
> 
>     app = QtGui.QApplication(sys.argv)
> 
>     ex = Example()
> 
>     ex.show()

Change this line,

 if drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction: 



More information about the Python-list mailing list