PyQt4 Notes – 1

Here are a few code snippets I had to search for . This is for my future reference and for any one who might needing it.

1 . First of all , a simple concept that I did not understand before.


app = QtGui.QApplication(sys.argv)

#Main Class here

sys.exit(app.exec_())  #app.exec_() runs the whole qt loop till you provide it with the exit method.

 

2 . And I had just started this thing called QtGraphicsView . It seems really complicated and this is what Ive understood till now.

#Inheriting the class QGraphicsView
class Example(QtGui.QGraphicsView):
    def  __init__ (self):
        super(Example , self).__init__() #As usual when you want to inherit the __init__ method of the class
        //Adding QGraphicsScene to your QGraphicsView which is the parent
        self.scene = QtGui.QGraphicsScene(self)

3 . And now you can add lines , ellipses and other stuff to your self.scene [For now I know just lines and ellipses]


self.line = QtGui.QGraphicsLineItem(self.startx , self.starty , self.endx , self.endy)
    //initial and final positions of the line.    self.scene.addItem(self.line)


4 . And for tracking the position of the mouse , the following functions come in handy


def mousePressEvent(self , event):
        //event is actually the class mousePressEvent        self.startx = event.x()
        self.starty = event.y()

The same applies for mouseReleaseEvents and mouseMoveEvents , the difference between event.x() and event.y() give the co-ordinates when the mouse is released or moved.

And if you want the clear the QGraphicsScene , what do you have to do?

Simple , self.scene.clear()

Thats all for now.

 

 

 

Advertisements

3 comments

  1. Hi Manoj,

    Appreciate your work,I need your help in one of your replied answer at stack,

    I am very new with pyqt,

    The below code of drag and drop in pyqt works well, but I have to drag multiple buttons with different name and connect them with wire, can you please help me,

    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()

    Thanks,
    Sachin

    1. Hey Sachin,

      I am really sorry, I do not do much of GUI programming nowadays. I’ll try to have a look later but don’t count too much on me.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: