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. Is it possible to auto connect all object's signals to another object?
QtWS25 Last Chance

Is it possible to auto connect all object's signals to another object?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 8.9k 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.
  • M Offline
    M Offline
    Maaaks
    wrote on last edited by
    #1

    Maybe the question is stupid, and the method which I search for is unuseful, but...

    I want a TheMostImportantClass instance to be informed about all signals emitted by all instances of SomeOtherClass. I know only one way to do it: to connect every SomeOtherClass's signal to TheMostImportantClass's slots manually in the constructor. But it's too boring. :-(

    Is there another way?

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      How do you want to connect them to the slots? Do you have a 1-to-1 mapping of signals to slots in your classes? Do you want all signals connected to a single slot or something in between?

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Maaaks
        wrote on last edited by
        #3

        bq. How do you want to connect them to the slots?

        I want to have something like the "Automatic Connections":http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html#automatic-connections feature in uic: signals should be automatically connected with specially named slots of TheMostImportantClass.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          It's not a feature of uic. It's a feature of QMetaObject :-)
          so you can use it in any QObject.

          see "here":http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html#widgets-and-dialogs-with-auto-connect

          @
          class myClass : public QObject
          {
          ...

          protected slots:
          void on_connectObj_signal1();
          void on_connectObj_signal2();
          void on_connectObj2_signal1();
          void on_connectObj2_signal2();

          private:
          ConnectObject* m_connectObj;
          ConnectObject2* m_connectObj2;
          };

          myClass::myClass()
          {
          m_connectObj = new ConnectObject; // which class ever :-)
          m_connectObj2 = new ConnectObject2; // which class ever :-)
          QMetaObject::connectSlotsByName(this);
          }
          @

          when ConnectObject and ConnectObject2 have a signal with signatur

          @
          void signal1();
          void signal2();
          @

          it should be automatically connected.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Maaaks
            wrote on last edited by
            #5

            It's not exactly the same that I'm looking for. Here, I must create a slot for every instance of ConnectObject and ConnectObject2. I want to have a slot which is called when a signal is emitted by any of registered ConnectObject's. Something like this:

            @class myClass : public QObject
            {
            ...

            protected slots:
            void on_connectObjects_signal1(ConnectObject* sender);
            void on_connectObjects_signal2(ConnectObject* sender);

            private:
            QList<ConnectObject*> m_connectObjects;
            };@

            I feel that it's simpler to make this myself. :-(

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              You can look at "QMetaObject":http://doc.qt.nokia.com/4.7/qmetaobject.html and use methodCount() and method() to inspect the slots and connect them to another object.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                ZapB
                wrote on last edited by
                #7

                As Volker said use QMetaObject and to make it simpler you could wrap up you object creation inside a factory class method that creates an object and then uses Volker's suggestion to iterate and make all the connections.

                Nokia Certified Qt Specialist
                Interested in hearing about Qt related work

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Or... simply make the connections from the TheMostImportantClass constructor, if you have control over that.

                  If all your instances of TheMostImportantClass have a limited number of parents (preferably all the same one), you might also use an event filter on those parents to look for the QChildEvent. If a child of type TheMostImportantClass is added, you can hook up your connections from there. That would allow you to do this without touching any other code in your application and without the need for a factory method. Note that if your TheMostImportantClass has a 0 parent, this approach will fail miserably.

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    ZapB
                    wrote on last edited by
                    #9

                    Yes that is a good approach too. It depends on the relationship of your objects as to which is more suitable. Anyway you have a few options to try now. :-)

                    Nokia Certified Qt Specialist
                    Interested in hearing about Qt related work

                    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