Circular Reference with Signals between widgets
-
I created a custom widget on top of QGraphicsView. When a key is pressed over the widget (let's say "c"), the widget maps the key press to the command "Change Color" which chooses the next random color for the objects in the scene. After making the change to the widget, it emits a signal indicating colorChange(<New Color>). Not a big deal.
I want to now have two instances of the widget. When one widget gets a color change, I want it to report to the other widget, so the other widget changes color. From the QMainWindow, I can connect these two together, but the problem is that a change in one widget will trigger a circular reference ... each window telling the other window about the change and responding accordingly. I figured that QSignalMapper is what I want to implement to coordinate the widgets, but I don't see how that will resolve the circular issue. In essence, I need some aspect of the emitted signal to indicate to the other widget that it shouldn't emit the changeColor signal. This issue needs to extend to >2 widgets as well.
To be transparent, I haven't implemented anything yet. I am still trying to work through in my head how I want to deal with it. I'm not being lazy. I'm just trying to figure it out before I try out a bunch of stuff. I have looked through this forum (and elsewhere online). I don't seem to see a lot of guidance suggesting that I must be missing something obvious.
Any insight is appreciated.
-
Emit the signal only when the color really changes.
-
Hi and welcome to devnet,
As @Christian-Ehrlicher suggested, the usual pattern is to check whether the new value is different from the old one and only update/emit a signal if it does. You can find that pattern used everywhere in Qt.
-
Thank you both for the response. There were some nuances which I neglected to mention, so that is my fault. In essence, the "changeColor" was a random color effect. As such, when the color changes randomly in one window, it triggers a random color change in another window (so the emitted signal back wouldn't necessarily be the same). I dropped the ball on that, but nonetheless, I appreciate the input; I'll look at that idea as the basis for how to track changes through signals.