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. Can I create an array of signals?
QtWS25 Last Chance

Can I create an array of signals?

Scheduled Pinned Locked Moved General and Desktop
signal & slotqthreadqobject
5 Posts 2 Posters 3.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.
  • T Offline
    T Offline
    tyler1991
    wrote on last edited by tyler1991
    #1

    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?

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      1
      • jsulmJ jsulm

        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.

        T Offline
        T Offline
        tyler1991
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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().

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          1
          • jsulmJ jsulm

            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().

            T Offline
            T Offline
            tyler1991
            wrote on last edited by
            #5

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

            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