How to create a QT signal dynamically?
-
wrote on 4 Jul 2022, 04:44 last edited by
Hi All,
I have a need to create QT signals dynamically in a for loop and will store in a map. I was told to make use of std::bind() method but bind method can be used for already existing functions. So how can I create the new functions and what is the correct method?
I am expecting something like below:
std::map<quint8, std::functionstd::string(std::string)> m_newSignals;
In my constructor I need:
for (quint8 loc_var=0; loc_var < final_value; loc_var++)
{
m_newSignals[loc_var] = std::bind(); //should generate new function
}In a different function I need to emit the new signals created as:
for (quint8 loc_var=0; loc_var < final_value; loc_var++)
{
l_functionToCall = m_newSignals.at(loc_var);
emit l_functionToCall(put parameters);
}Can someone help with this?
-
Hi All,
I have a need to create QT signals dynamically in a for loop and will store in a map. I was told to make use of std::bind() method but bind method can be used for already existing functions. So how can I create the new functions and what is the correct method?
I am expecting something like below:
std::map<quint8, std::functionstd::string(std::string)> m_newSignals;
In my constructor I need:
for (quint8 loc_var=0; loc_var < final_value; loc_var++)
{
m_newSignals[loc_var] = std::bind(); //should generate new function
}In a different function I need to emit the new signals created as:
for (quint8 loc_var=0; loc_var < final_value; loc_var++)
{
l_functionToCall = m_newSignals.at(loc_var);
emit l_functionToCall(put parameters);
}Can someone help with this?
@Savitha you can't create Signals dynamically.
Every single one of them has to be declared at compile time, and as an additionally limitation by MOC, templates do also not work.
The closed thing you can do is store your signals in an array and access them later on that way, see here for an example that I made
https://forum.qt.io/topic/128026/is-it-possible-to-mimic-the-functionality-of-an-array-of-signals/12
-
@Savitha you can't create Signals dynamically.
Every single one of them has to be declared at compile time, and as an additionally limitation by MOC, templates do also not work.
The closed thing you can do is store your signals in an array and access them later on that way, see here for an example that I made
https://forum.qt.io/topic/128026/is-it-possible-to-mimic-the-functionality-of-an-array-of-signals/12
wrote on 4 Jul 2022, 05:30 last edited by@J-Hilk Thanks for the reply, I will check your sample code
-
wrote on 5 Jul 2022, 01:07 last edited by
There shouldn't be a need for dynamic signals since the signal/slot mechanism allows parameter passing. a single parametized signal allows the receiving slot to understand the context in which the signal was sent.
1/4