Re: pyqt problem

2014-10-02 Thread Sachin Tiwari
On Thursday, October 2, 2014 3:19:22 PM UTC+5:30, Sachin Tiwari wrote:
> Hi 
> 
> I am learning pyqt, can any one help me to make instances of pushbutton 
> wherever cursor will be clicked on canvas,like a circuit simulator where  we 
> add components on canvas just by left or right click.
> 
> 
> 
> Thanks & Regards,
> 
> Sachin

Hi

swch = QPushButton('', self)
swch.setToolTip('Switch ')
swch.setCheckable(True)
swch.setIcon(QIcon('sw.png'))
swch.setIconSize(QSize(40,40))
swch.move(05, 25)
swch.clicked.connect(self.switch)

The above code will make a push button on canvas, now what I want when I will 
click on this button it get ready to drop anywhere on canvas by left click,I 
mean if I click 10 times on canvas 10 push button will be there.
 

-- 
https://mail.python.org/mailman/listinfo/python-list


pyqt darg and drop

2014-10-04 Thread Sachin Tiwari
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()
 

   
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pyqt darg and drop

2014-10-04 Thread Sachin Tiwari
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: 
-- 
https://mail.python.org/mailman/listinfo/python-list