Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78686 > unrolled thread

pyqt darg and drop

Started bySachin Tiwari <sachin.tiwari50@gmail.com>
First post2014-10-04 03:05 -0700
Last post2014-10-04 03:32 -0700
Articles 2 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  pyqt darg and drop Sachin Tiwari <sachin.tiwari50@gmail.com> - 2014-10-04 03:05 -0700
    Re: pyqt darg and drop Sachin Tiwari <sachin.tiwari50@gmail.com> - 2014-10-04 03:32 -0700

#78686 — pyqt darg and drop

FromSachin Tiwari <sachin.tiwari50@gmail.com>
Date2014-10-04 03:05 -0700
Subjectpyqt darg and drop
Message-ID<54356bfb-b05c-4f18-a855-9276297b0eee@googlegroups.com>
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()
                                     

                                                                           

[toc] | [next] | [standalone]


#78689

FromSachin Tiwari <sachin.tiwari50@gmail.com>
Date2014-10-04 03:32 -0700
Message-ID<57535a33-01ef-4b19-8b00-2e24b0ae8ae7@googlegroups.com>
In reply to#78686
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: 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web