Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Circular Reference with Signals between widgets

Circular Reference with Signals between widgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 461 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Holmesd3
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Emit the signal only when the color really changes.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Holmesd3
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved