Can I create an array of signals?



  • Here is the scenario:

    I have a QObject which spawns 5 QThreads. Each of those threads has an instance of a QObject class I designed. I would like to communicate to each QObject instance from the original QObject using signals and slots. I know this can easily be accomplished. Create 5 signals and connect each signal to a slot of a QObject on a QThread. However, what if instead I wanted to spawn 6, 8, or 10 QThreads. Is there a way to connect a independent signal to each of these new threads without having to manually write new code for the new signals? I would to to create a signal array which I can set the size to match the number of threads I would like to spawn. However, I have found no such functionality in the Qt documentation.

    Can someone help me out?


  • Moderators

    Why do you need one signal for each of the threads?
    You can use one signal and pass a parameter to it which tells the threads which one of them should react.



  • @jsulm I considered that solution. However, my program is a real-time application, so I thought having each thread respond every time I sent a signal was too inefficient.


  • Moderators

    Then you should not use signals. Use http://doc.qt.io/qt-5.5/qmetaobject.html#invokeMethod to call a method on a specific thread. Signals are usually used if the class emitting the signal does not care who listen to this signal. In your case you know which thread you want to access, so just call a method directly via invokeMethod().



  • @jsulm Thanks! I'll give it a try.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.