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



  • 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


  • Lifetime Qt Champion

    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


  • Moderators

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



  • [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



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


  • Lifetime Qt Champion

    Are you thinking about something like QSignalMapper ?



  • [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.



  • [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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.