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. differences Signal Slot vs function
Forum Updated to NodeBB v4.3 + New Features

differences Signal Slot vs function

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 7.1k Views 3 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.
  • Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by
    #5

    Unfortunately it's the same question as "how can I get every instance of some class in c++". In general you can't. It might be possible if you put some restrains on the class.
    For example - if you make all the instances children of a single object you could use findChildren() on it to get them. Other way is to create the instances via a factory and that factory could take care of connections.
    In any case you need a single place that knows about all of them. That's just how c++ works.

    1 Reply Last reply
    1
    • QT-static-prgmQ Offline
      QT-static-prgmQ Offline
      QT-static-prgm
      wrote on last edited by
      #6

      Ok, that's bad. I just hoped there is a way to have a kind of public static signal, that can be used to connect and so every instance of the class emit that signal will call the connected slot.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #7

        Would it be impossible or too complicated to create one delgator object and delegate the signal through that? For example Sender, Delegator, Receiver.

        class Delegator // QObject stuff...
            signals: void someSignal();
            public slots: void someSlot();
        }
        
        Sender::Sender(Delegator d) {
            connect(/*this to d's slot*/.... );
        }
        Receiver::Receiver(Delegator d){
            connect(/*d's signal to slot of this);
        }
        
        1 Reply Last reply
        1
        • E Offline
          E Offline
          Eeli K
          wrote on last edited by Eeli K
          #8

          Now when I think of it, it should be possible to leave the delegator out and just connect directly to the receiver, if there is one receiver object which exists when you create senders.

          Sender::Sender(Receiver* r) { // of course a pointer or reference, not copy, as in previous post...
              connect(/* signal of this to slot of r*/....);
          }
          
          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #9

            @Eeli-K Your first example is basically what QSignalMapper does, except there is no coupling between the receiver and the delegator (that's bad design). Similarly - your second code creates coupling between sender and receiver. That's not good. You need a 3rd party to manage the connections . Sender and receiver should not know about each other.

            A pseudocode with a factory would look like this:

            Receiver* receiver = ...;
            SenderFactory* factory = ...; //emits a createdInstance signal when it creates an instance
            
            connect(factory, &SenderFactory::createdInstance, [=](Sender* obj){
               connect(obj, &Sender::someSignal, receiver, &Receiver::someSlot);
            });
            
            SenderObject* sender = factory.createInstance(); 
            //this now automatically connects sender signal to receiver slot without sender or receiver knowing about themselves.
            //factory is the enabler of this mechanism and your own code is the 3rd party that makes the connections
            
            1 Reply Last reply
            3
            • QT-static-prgmQ Offline
              QT-static-prgmQ Offline
              QT-static-prgm
              wrote on last edited by
              #10

              ok, looks a bit complicate, I'll look into that when i fixed my other problems. For now i made it that way

              MainWindow* tmp = dynamic_cast<MainWindow*>(parent->parent()->parent());
              if(tmp != NULL)
              	connect(this, SIGNAL(sendMessage(QString, int)), tmp, SLOT(showMessage(QString, int)));
              

              But i thought there is maybe a more elegant way.

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #11

                Hi
                just a note:
                (parent->parent()->parent());
                is dangerous. as if parent->parent() return NULL u can crash
                as NULL->parent() is not fun.
                Might be ok in your case so just a note :)

                1 Reply Last reply
                3
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12
                  MainWindow* tmp = dynamic_cast<MainWindow*>(parent->parent()->parent());
                  

                  Apart from the potential crash that @mrjj mentioned that looks like terrible design. Be prepared that at some point the structure of parents will change and you'll spend some quality time debugging "Why on earth did my connection stop working? I haven't changed anything with it..."

                  1 Reply Last reply
                  1
                  • QT-static-prgmQ Offline
                    QT-static-prgmQ Offline
                    QT-static-prgm
                    wrote on last edited by
                    #13

                    That's why i asked for static signals :P

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      Yeah, there are no static signals but that doesn't justify such awful solution. What you want can be achieved without ugly hacks (I mentioned one possible way but there are others).
                      Semantically speaking static signal would have no meaning, as a signal indicates a change in state of some object. If there is no object the meaning is lost.

                      1 Reply Last reply
                      2
                      • QT-static-prgmQ Offline
                        QT-static-prgmQ Offline
                        QT-static-prgm
                        wrote on last edited by QT-static-prgm
                        #15

                        @Chris-Kawa i'm currently cleaning my code to make it more readable and to improve the performance.
                        and so i came back to my signal slot problem here and the really really bad implementation using parent()->parent()->parent()
                        I ddidn't find a good way to implement your factory solution, but i found an other way. So maybe you can take a look at it and tell me if this is a good implementation or not.

                        In words:
                        i made a new class OutputDevice as singleton and get the first instance of it in my MainWindow. I set the parent and connect the sendMessage with my print function.
                        In all other classes i removed the sendMessage signal and now i call OutputDevice::getInstance()->print(QString, int); That way my fileinterface is no longer a QObject and i don't need all the signals anymore.

                        In code (only the commit changes):
                        https://git.rwth-aachen.de/carstenf/OpenGL/commit/98302664ca365c838c9c585f87d0d4229780976e

                        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