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. Signal from anonymous object
Forum Updated to NodeBB v4.3 + New Features

Signal from anonymous object

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 3.3k 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.
  • O Offline
    O Offline
    ofmrew
    wrote on 29 Mar 2017, 15:29 last edited by A Former User 4 Jan 2017, 10:36
    #1

    I have a class MyRunnable that uses multiple inheritance, QObject and QRunnable, which emit signals and runs on thread provided by QThreadPool global instance. I want to create more instances of this class and have indicators for number created, waiting to run, running and completed. The problem is how to connect the signals to the slots since I do not keep a reference to the objects. The class of the object emitting the signal is known, but the object is anonymous, namely: connect(????, &MyRunnable::someSignal, this, &MainWindow::handleSignal). What should be substituted for ????.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Mar 2017, 15:51 last edited by
      #2

      Hi
      ???? must be an instance pointer as far as i know.
      Cant you connect it up when you create the instances ?

      O 1 Reply Last reply 29 Mar 2017, 17:38
      1
      • V Offline
        V Offline
        VRonin
        wrote on 29 Mar 2017, 15:55 last edited by mrjj
        #3

        can you show a minimal example of what you are doing?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • M mrjj
          29 Mar 2017, 15:51

          Hi
          ???? must be an instance pointer as far as i know.
          Cant you connect it up when you create the instances ?

          O Offline
          O Offline
          ofmrew
          wrote on 29 Mar 2017, 17:38 last edited by
          #4

          @mrjj

          Say I create one thousand instances, I surely do not want to create a connect statement for each. The Qt documentation seems contradictory it that it says that any object can emit the signal, but all examples plainly show a object as the first parameter. My question is does the connect statement connect an object to the signal or does it connect the class, thus allowing any object of that class to signal and have the connect statement correctly handle the connection between the object and the slot.

          I will try two experiments: 1. I will create an arbitrary MyRunnable to see if that makes the connection and then, if it does, see if any other MyRunnable object connects correctly. 2. I will create another, MySignaler, class an create a singleton and add that to each MyRunnable object and see if the connections to that object works. Will let you know.

          V 1 Reply Last reply 29 Mar 2017, 18:41
          0
          • O ofmrew
            29 Mar 2017, 17:38

            @mrjj

            Say I create one thousand instances, I surely do not want to create a connect statement for each. The Qt documentation seems contradictory it that it says that any object can emit the signal, but all examples plainly show a object as the first parameter. My question is does the connect statement connect an object to the signal or does it connect the class, thus allowing any object of that class to signal and have the connect statement correctly handle the connection between the object and the slot.

            I will try two experiments: 1. I will create an arbitrary MyRunnable to see if that makes the connection and then, if it does, see if any other MyRunnable object connects correctly. 2. I will create another, MySignaler, class an create a singleton and add that to each MyRunnable object and see if the connections to that object works. Will let you know.

            V Offline
            V Offline
            VRonin
            wrote on 29 Mar 2017, 18:41 last edited by VRonin
            #5

            Say I create one thousand instances, I surely do not want to create a connect statement for each.

            yes... but it's the same for all of them...

            @ofmrew said in signal from anonymous object:

            create a singleton

            singleton in multi-threaded environment is asking for trouble.

            Could you show us in a few lines of code what you'd like to achieve?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • 6 Offline
              6 Offline
              6thC
              wrote on 30 Mar 2017, 00:05 last edited by
              #6

              Say I create one thousand instances, I surely do not want to create a connect statement for each.

              It could be a stupid idea, but I know I wouldn't code 1K connects myself, but I have no issue if the code does. If the application performs well. etc.

              Could you put the connect statement inside the object and maybe at CTOR time pass an reference / pointer to the object you wish to connect SLOT to?

              That way the connect works and you only need to code one connect(); but it gets used as much as you instance up new objects?

              Be interesting to see how much how fast you could throw at this. If you do go that way and do any testing can you let us know?

              J 1 Reply Last reply 30 Mar 2017, 04:37
              0
              • 6 6thC
                30 Mar 2017, 00:05

                Say I create one thousand instances, I surely do not want to create a connect statement for each.

                It could be a stupid idea, but I know I wouldn't code 1K connects myself, but I have no issue if the code does. If the application performs well. etc.

                Could you put the connect statement inside the object and maybe at CTOR time pass an reference / pointer to the object you wish to connect SLOT to?

                That way the connect works and you only need to code one connect(); but it gets used as much as you instance up new objects?

                Be interesting to see how much how fast you could throw at this. If you do go that way and do any testing can you let us know?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 30 Mar 2017, 04:37 last edited by
                #7

                @6thC said in signal from anonymous object:

                It could be a stupid idea, but I know I wouldn't code 1K connects myself, but I have no issue if the code does

                Why not do this in a loop?

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

                6 1 Reply Last reply 30 Mar 2017, 05:03
                0
                • J jsulm
                  30 Mar 2017, 04:37

                  @6thC said in signal from anonymous object:

                  It could be a stupid idea, but I know I wouldn't code 1K connects myself, but I have no issue if the code does

                  Why not do this in a loop?

                  6 Offline
                  6 Offline
                  6thC
                  wrote on 30 Mar 2017, 05:03 last edited by 6thC
                  #8

                  @jsulm
                  That was the suggestion.

                  I didn't really specify a loop or assume to know the circumstances or demand to create instances.

                  I was just pointing out that if you defined the connect contained inside your object - should work regardless of what creates them and not have to code a million connects (even if that is what is the actual result of your code).

                  The trade off is that you would need to pass it the reference of pointer to the other class so the connect can get it's required slot context/arguments. But I expect that would work.

                  J 1 Reply Last reply 30 Mar 2017, 05:07
                  0
                  • 6 6thC
                    30 Mar 2017, 05:03

                    @jsulm
                    That was the suggestion.

                    I didn't really specify a loop or assume to know the circumstances or demand to create instances.

                    I was just pointing out that if you defined the connect contained inside your object - should work regardless of what creates them and not have to code a million connects (even if that is what is the actual result of your code).

                    The trade off is that you would need to pass it the reference of pointer to the other class so the connect can get it's required slot context/arguments. But I expect that would work.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 30 Mar 2017, 05:07 last edited by jsulm
                    #9

                    @6thC Well, the amount of connects does not depend on the way you implement it: whether you use a loop or put the connect into your object - you have the same number of connect() calls. And using a loop you do not code millions of connects by hand (or what do you mean?): actually you can do it with two lines of code (pseudocode):

                    for(...)
                        connect(sender[i], signal, receiver[i], slot);
                    

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

                    6 1 Reply Last reply 30 Mar 2017, 05:44
                    0
                    • J jsulm
                      30 Mar 2017, 05:07

                      @6thC Well, the amount of connects does not depend on the way you implement it: whether you use a loop or put the connect into your object - you have the same number of connect() calls. And using a loop you do not code millions of connects by hand (or what do you mean?): actually you can do it with two lines of code (pseudocode):

                      for(...)
                          connect(sender[i], signal, receiver[i], slot);
                      
                      6 Offline
                      6 Offline
                      6thC
                      wrote on 30 Mar 2017, 05:44 last edited by
                      #10

                      @jsulm
                      sure, you could do that in a loop. I wasn't saying otherwise.
                      I was suggesting the same effect though - code it once and use it as many times as you need.

                      I was thinking connect inside the object at a glance seems flexible and transports with that object. Things could come and go as they please with ease.

                      Obviously requires an object - but that was going to happen anyhow in this case right?
                      Inside an object would only be the one line of code, the CTOR would be as:

                      connect(this, &MyRunnable::someSignal, targetObject, &TargetObject::handleSlot);
                      
                      J 1 Reply Last reply 30 Mar 2017, 05:56
                      0
                      • 6 6thC
                        30 Mar 2017, 05:44

                        @jsulm
                        sure, you could do that in a loop. I wasn't saying otherwise.
                        I was suggesting the same effect though - code it once and use it as many times as you need.

                        I was thinking connect inside the object at a glance seems flexible and transports with that object. Things could come and go as they please with ease.

                        Obviously requires an object - but that was going to happen anyhow in this case right?
                        Inside an object would only be the one line of code, the CTOR would be as:

                        connect(this, &MyRunnable::someSignal, targetObject, &TargetObject::handleSlot);
                        
                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 30 Mar 2017, 05:56 last edited by
                        #11

                        @6thC Sure, you can do this, but then you need to pass the pointer to the other object - you create dependencies between classes this way.
                        Signals/slots are often used to make different classes more independent of each other: the sender does not need to know who will connect to its signals and the receiver does not necessarily need to know who sends the signals.

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

                        6 1 Reply Last reply 30 Mar 2017, 06:11
                        1
                        • J jsulm
                          30 Mar 2017, 05:56

                          @6thC Sure, you can do this, but then you need to pass the pointer to the other object - you create dependencies between classes this way.
                          Signals/slots are often used to make different classes more independent of each other: the sender does not need to know who will connect to its signals and the receiver does not necessarily need to know who sends the signals.

                          6 Offline
                          6 Offline
                          6thC
                          wrote on 30 Mar 2017, 06:11 last edited by
                          #12

                          @jsulm
                          I see what you were getting at. Yes loosely coupled is ideal.

                          I honestly haven't even done how I suggested, I don't have thousands of connects but I do have a a lot but not thousands.

                          So, thinking about it you're absolutely right. I would do it outside of that object after all.

                          My app I have an app object like:
                          MyApp : public QApplication
                          and all my other classes are instanced here as members, so my connects are all defined inside that and everything is sweet.

                          If I had to do thousands of a class, yes, I would expect I'd do it there also and Iterating a container of <T> would be legit and easy.

                          How do you design your IOC like code, do you use an application lifetime object, or do you have some other design concept/pattern?
                          Just wondering, most of what I have is just what I've learnt myself...

                          1 Reply Last reply
                          0
                          • O Offline
                            O Offline
                            ofmrew
                            wrote on 30 Mar 2017, 13:31 last edited by
                            #13

                            My statement about 1000 objects should not be taken literally. What I was trying to demonstrate was how a thread pool could queue runnable objects when the number of runnable objects exceeded the number of processor cores (or hyper threads) or the setting of max threads by having a button that created and started a runnable object—and the button could be pressed 1000 time very fast. (It has no useful value other that to explore what information could be obtained about the states of the runnable objects.) The problem would have been simple it the thread pool class had a signal for a thread completion, but the thread pool object is opaque about its internals. Without that signal the only means was to have the thread do the signaling.

                            For the discussion and research I did, I concluded that the only way that connect could be used was if it could be built and destroyed as required. If that can be done and, if so, how to accomplish it will require more research.

                            Thank you for your responses. This item is resolved.

                            1 Reply Last reply
                            0

                            1/13

                            29 Mar 2017, 15:29

                            • Login

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