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. How to receive signals without knowing the sender ? [Solved]
Forum Update on Monday, May 27th 2025

How to receive signals without knowing the sender ? [Solved]

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 4.0k 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.
  • A Offline
    A Offline
    archqt
    wrote on last edited by
    #1

    Hi,
    i have this problem. I have several objects (more than 30), to simplify i will say Buttons, which are derivated from QPushButton. On theses buttons i catch the clicked() signal and then i emit a signal message(QString) with simply the text on the button.
    I would like to catch this signal without connecting all the objects to the receiver. For now i connected the signal in the button to the parents (in the constructor). Like this a button is automaticaly connected to the MainWindow.
    But the problem is that i won't be able to use the Button in other places because they are "connected" to the receiver.
    Is there a way to catch signals or event with informations witout knowing the sender ? I would like to write a thing like connect(SIGNAL(QString), this, SLOT(myslot()));
    Thanks

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

      Hi,

      No you can't, you need a source for the signal. However you can use findChildren and loop trough the list of QPushButton to connect them to your slot.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi,

        [quote author="archqt" date="1415309827"]But the problem is that i won't be able to use the Button in other places because they are "connected" to the receiver.[/quote]I don't understand this bit. Could you please explain in more detail what this means? What "other places" need to use the buttons?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • A Offline
          A Offline
          archqt
          wrote on last edited by
          #4

          [quote author="JKSH" date="1415318240"]Hi,

          [quote author="archqt" date="1415309827"]But the problem is that i won't be able to use the Button in other places because they are "connected" to the receiver.[/quote]I don't understand this bit. Could you please explain in more detail what this means? What "other places" need to use the buttons?[/quote]

          For now i did this
          @#include "mainwindow.h"

          MyButton::MyButton(QWidget parent) :
          QPushButton(parent)
          {
          connect(this,SIGNAL(clicked()),this,SLOT(click_slot()));
          connect(this,SIGNAL(message(QString)),static_cast<MainWindow
          >(parent->parent()),SLOT(receive_message(QString)));
          }

          void MyButton::click_slot()
          {
          emit message(text());
          }@

          But the fact is that i must know the slot name and the receiver, so it is not easy to re_use this Component (MyButton) in another project without modifying the code. And i must know where the "MainWindow" is in the hierarchy class.
          Sincerely

          1 Reply Last reply
          0
          • A Offline
            A Offline
            archqt
            wrote on last edited by
            #5

            [quote author="SGaist" date="1415312207"]Hi,

            No you can't, you need a source for the signal. However you can use findChildren and loop trough the list of QPushButton to connect them to your slot.

            Hope it helps[/quote]

            I was expecting to avoid this solution. Thanks for the answer

            1 Reply Last reply
            0
            • W Offline
              W Offline
              wrosecrans
              wrote on last edited by
              #6

              If you really wanted to go down this road, you could have a global channel coordinator singleton. I wouldn't really suggest going that way, but you could have a simple api where your qPushButton subclass does en "emit broadcast(ChannelName);" whenever it is clicked. The constructor for the button connects the broadcast signal to a slot on the global singleton. Anything that needs to listen for this global broadcast signal connects the singleton to their own slot, and looks for a message that they care about.

              But, it's much better to design things in a way that you know what connects to what.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Are you thinking about something like QSignalMapper ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                  [quote author="SGaist" date="1415399224"]Are you thinking about something like QSignalMapper ?[/quote]

                  Yes. What i wanted to avoid what the fact that i have to connect every button to another thing. But thanks for the answer i will try to see if this is easier than my solution.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    archqt
                    wrote on last edited by
                    #9

                    [quote author="wrosecrans" date="1415393086"]If you really wanted to go down this road, you could have a global channel coordinator singleton. I wouldn't really suggest going that way, but you could have a simple api where your qPushButton subclass does en "emit broadcast(ChannelName);" whenever it is clicked. The constructor for the button connects the broadcast signal to a slot on the global singleton. Anything that needs to listen for this global broadcast signal connects the singleton to their own slot, and looks for a message that they care about.

                    But, it's much better to design things in a way that you know what connects to what. [/quote]

                    Thanks.

                    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