Important: Please read the Qt Code of Conduct -

How to emit a signal to all QGraphicsRectItem

  • Hi,
    I have subclass the QGraphicsRectItem (MyQGraphicsRectItem) to produce different type of QGraphicsRectItem and I have set the flag setAcceptHoverEvents(true);
    Now, when the mouse is over on a MyQGraphicsRectItem I'd like to emit a signal to all MyQGraphicsRectItems in scene and paint the background-color red or green according to the type of MyQGraphicsRectItem.

    There is a way to emit a "signal in broadcast" to all my derived class "MyQGraphicsRectItem"?

    Thank you

  • Qt Champions 2019

    Since QGraphicsItem is not derived from QObject you can't send a signal (until you derive from QObject, but you did not mentioned it).
    Since you create the items you can put them in a container and iterate over them later on to set the appropriate flag.

  • This is an interesting question which seems to have a more direct answer of "no, there is no universal signal broadcast mechanism in Qt"? I believe Qt is a pure publisher/subscriber architecture.

    Could a similar effect be accomplished using framework events instead of signals?

  • Lifetime Qt Champion

    @Kent-Dorfman It's just that it does not make sense for QGraphicsItem to be QObject based. There's a cost for that and since you may have a lot of them in a scene and usually you don't need all that machinery, it's better to keep them from being QObject based (same goes for the item classes related to QTree/QTableWidget.

    Note that there is QGraphicsObject if you need items with QObject capabilities.

  • @SGaist said in How to emit a signal to all QGraphicsRectItem:

    Note that there is QGraphicsObject if you need items with QObject capabilities.

    Or, if you want to keep the QGraphicsRectItem capabilities, you can derive from QObject first, QGraphicsRectItem second. Although I would probably prefer a solution not using QObject/signals, unless there is a good reason for it.

  • OK, thank you all very much.

    Yes, my derived class is something like that, because I need the signal/slot mechanism

    class VTGraphicsPin : public QObject, public QGraphicsRectItem

    I think I'll send a signal to an external class (manager) because it has a container, and iterate on all objects to check what "pin" should have the same color.

Log in to reply