[PyQt] QGraphicsView item select

Andreas Pakulat apaku at gmx.de
Wed Apr 11 14:28:49 BST 2012


On 11.04.12 06:02:09, Alfredo Junior wrote:
> OK.
> 
> This is a functional example:
> 
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
> 
> import sys
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> 
> def itemSelected(self):
>     print "Selection changed"
>     
> app = QApplication(sys.argv)
> grview = QGraphicsView()
> scene = QGraphicsScene()
> scene.addPixmap(QPixmap('on.png'))
> grview.setScene(scene)
> scene.selectionChanged.connect(itemSelected)
> grview.show()
> sys.exit(app.exec_())
> 
> It should run the function itemSelected and print "Selection changed" when I click on the picture (little green square).

Indeed this example does not trigger the the selection change signal. In
the docs of the signal is a pointer to QGraphicsItem::setSelected and a
pointer to QGraphicsScene::setSelectionArea and both functions document
that items are not selectable by default, so it needs to be enabled by
setting the corresponding flag on the item.

Andreas



More information about the PyQt mailing list