Search code examples
qt4

how connect different signals to the same slot on QT?


I'm trying to connect different signals to the same slot like this:

connect(thread_notification,SIGNAL(signal1()),SIGNAL(signal2()),this,SLOT(slot()));

is it possible?


Solution

  • Yes it's possible. You can do it by simple write a connection statement for each signal you want to send to your slot, like:

    connect(thread_notification, SIGNAL(signal1()), this, SLOT(slot()));
    connect(thread_notification, SIGNAL(signal2()), this, SLOT(slot()));
    ... 
    connect(thread_notification, SIGNAL(signalN()), this, SLOT(slot()));
    

    Another way you can follow, especially if the signals' senders are array, list or, in general, containers of objects, is to use QSignalMapper, but it depends on who sends and what signal is sent. You can find an explenation here http://doc.qt.io/qt-4.8/qsignalmapper.html