QGraphicsitem emit signal when selected/unselected

2019-05-12 17:02发布

问题:

Could any one gives some hint on the easy way to get a signal emitted when the QGraphicsitem is selected/unselected each time?

回答1:

You can use itemChange() to get notified of that (or emit your own signal if you really need it from there):

QVariant QGraphicsItem::itemChange ( GraphicsItemChange change, const QVariant & value ) [virtual protected]

more or less like (pseudocode)

QVariant QGraphicsItemSubclass::itemChange( GraphicsItemChange change, 
                                                 const QVariant &value ) {
    if ( change == QGraphicsItem::ItemSelectedChange ) {
        if (value == true) {
          // Handling selection.. / selection emission