Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to emit a signal in whole of project?
QtWS25 Last Chance

How to emit a signal in whole of project?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
qsocketnotifieremit signal
5 Posts 4 Posters 7.2k Views
  • 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.
  • MhM93M Offline
    MhM93M Offline
    MhM93
    wrote on last edited by
    #1

    Hi.
    I receive data from QSocketNotifier in mainwindow.cpp . I want to emit signal when receive data in whole of application.
    Then each class that they need this signal, call their own slot to do something.
    How to define a global signal that work in whole of project?

    H.Ghassami

    1 Reply Last reply
    0
    • miclandM Offline
      miclandM Offline
      micland
      wrote on last edited by
      #2

      There is no "broadcast" mechanism to send a signal to every object. You have to connect every slot explicitely.
      So you have to make your QSoketNotifier global accessible and every object can access the instance and connect to the signal himself if wanted.
      (That's possible but not a clean solution - you should try to rework your code so that the signal emitting object does not need to know the receivers and the signal receiving objects do not need to know the emitter and some glue code initializes all instances and connects the signals and slots).

      1 Reply Last reply
      1
      • Hamed.MasafiH Offline
        Hamed.MasafiH Offline
        Hamed.Masafi
        wrote on last edited by
        #3

        Create a class with singleton and observer pattern.

        Remote object sharing (OO RPC)
        http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

        Advanced, Powerful and easy to use ORM for Qt5
        https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

        kshegunovK 1 Reply Last reply
        1
        • Hamed.MasafiH Hamed.Masafi

          Create a class with singleton and observer pattern.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @MhM93

          Hello,
          @micland Has provided some good points. I think however that the cleanest solution to a problem like this is to delegate the signals through the object hierarchy (and not to expose the original emitter to every object). So every parent object will have a signal, that the children will subscribe to, which in turn will raise the child's signal and so on.
          For (a simple) example:

          class PropagatingSignalClass : public QObject
          {
              Q_OBJECT
          public:
              PropagatingSignalClass(QObject * parent)
                  : QObject(parent)
              {
                  PropagatingSignalClass * interestingParent = qobject_cast<PropagatingSignalClass *>(parent);
                  if (interestingParent)
                      QObject::connect(interestingParent, SIGNAL(someInterestingSignal()), this, SIGNAL(someInterestingSignal()));
              }
          
          signals:
              void someInterestingSignal();
          }
          

          When the top-level QObject's signal is triggered such structure will carry the signal down the object tree and isn't too hard to implement.

          Kind regards.


          @Hamed.Masafi

          Create a class with singleton and observer pattern.

          Why should he do that exactly? By the way a slot (connected to a signal) is already an observer pattern.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • MhM93M Offline
            MhM93M Offline
            MhM93
            wrote on last edited by
            #5

            Thanks for all. I should test it , if it works I will put the code completely here.

            H.Ghassami

            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