Good Day all,

I have a little problem I hope somebody can shed some light on, In the
attached python script I have set up a QGraphicsScene and added a couple
of items to it, then I set the items flags to ItemIsSelectable.

imagine this as a kind of buddy list type thing, when an item is
selected the item is animated and the text turns blue. however when I
right click to bring up my context menu it seems to select none. when
the item is blue and selected I'd like to just be able to right click on
it and show my context menu without having to reselect it in my
contextMenuEvent with item.setSelected(1).

the program is small so one can try it and see, I just don't want it to
animate on right click basically (unless it is actually being selected).

Thanks in advance

Martin


# -*- coding: utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtOpenGL import *

class Animation(QPropertyAnimation):
    def __init__(self, target,prop, posi):
        super(Animation, self).__init__(target,prop)
        self.setEasingCurve(QEasingCurve.OutBounce)
        self.setStartValue(QPointF(-150, posi))
        self.setEndValue(QPointF(0, posi))
        self.setDuration(1500)

class Item(QGraphicsObject):
        def __init__(self, name, parent=None):
                super(Item, self).__init__(parent)
                self.nick = name
                self.setData(0, QVariant(self.nick))
                self.setFlag(QGraphicsItem.ItemIsSelectable, 1)
                self.setData(1, QVariant(0))
        def boundingRect(self):
                return QRectF(0,0,120,60)
        def paint(self, painter, option, widget):
                if self.isSelected():
                        painter.setPen(Qt.blue)
                else:
                        painter.setPen(Qt.black)
                painter.drawRect(0,0,120,60)
                painter.drawPixmap(1,1,QPixmap("me.jpg.thumbnail"))
                painter.drawText(80,30,self.nick)

class Window(QWidget):
        def __init__(self, parent=None):
                super(QWidget, self).__init__(parent)
                self.scene = QGraphicsScene()
                QObject.connect(self.scene, SIGNAL("selectionChanged()"), 
self.animateItem)
                self.scene.setSceneRect(0, 0, 130, 300)
                self.item = Item("Martin")
                self.itemb = Item("Guest")
                self.scene.addItem(self.item)
                self.scene.addItem(self.itemb)
                self.grview = QGraphicsView(self)
                self.grview.setViewport(QGLWidget())
                self.grview.setScene(self.scene)
                self.anim = Animation(self.item, 'pos', 0)
                self.animb = Animation(self.itemb, 'pos', 60)
                self.anim.start()
                self.animb.start()
                self.menu = QMenu()
                self.chatAction = QAction("Chat to", None)
                self.menu.addAction(self.chatAction)

        def animateItem(self):
                self.selitem = self.scene.selectedItems()
                if self.selitem:
                        self.rot = QGraphicsRotation()
                        self.rot.setAxis(Qt.YAxis)
                        self.rot.setOrigin(QVector3D(QPointF(60,30)))
                        self.selitem[0].setTransformations([self.rot])
                        self.item_anim = QPropertyAnimation(self.rot, 'angle')
                        self.item_anim.setEasingCurve(QEasingCurve.OutCirc)
                        self.item_anim.setStartValue(0)
                        self.item_anim.setEndValue(360)
                        self.item_anim.setDuration(1500)
                        self.item_anim.start()

        def contextMenuEvent(self, event):
                self.obj = self.scene.itemAt(event.x(), event.y())
                if self.obj != None:
#                       self.obj.setSelected(1)
                        self.chatAction.setText("Chat to " + 
self.obj.data(0).toString())
                        self.menu.exec_(QCursor.pos())



if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = Window()
        w.resize(130,300)
        w.show()
        sys.exit(app.exec_())
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to