0

While trying to do something like this:

connect(process1, SIGNAL(waitForReadyRead()), this, SLOT(dataReceived_1()));

i have way too many processes for signals. I have created a QList with all those processes (objects).

In order to simplify the code, i wanted to put this connect in a loop and perform this signal slot connection by iterating through the processList.

Something like this:

for(int i=0; i<procList.length(); ++i){
   connect(procList[i], SIGNAL(waitForReadyRead()), this, SLOT(data_rcvd[i]));
}

Here data_rcvd is a QList containing the slot functions.~ but unfortunately we can not add functions inside QList. So i want to know, is there a possibility to define function pointers to achieve this task???

Or you think, i create a new QProcess class (extend) and overload the function waitForReadyRead() to emit a pointer containing this specific information maybe?

1 Answers1

0

A list may not contain functions but it may contain function pointers. So declare a list of function pointers . In expressions function designators implicitly are converted to pointers to the function types. Pointers are objects.

Here is a demonstrative program that uses the standard container std::list.

#include <iostream>
#include <list>

long long sum( int x, int y ) { return ( long long )x + y; }
long long multiply( int x, int y ) { return ( long long )x * y; }

int main()
{
    std::list<long long( * )( int, int )> lst = { sum, multiply };

    for ( const auto &p : lst ) std::cout << p( 10, 2 ) << '\n';
}

Its output is

12
20
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Can you give an example for this? – johny bravo Jul 23 '19 at 08:54
  • thanks it works fine in general but when i define the function within a class (non static) then it does not compile. I always get this error: reference to non static member function must be called. I can not define function as slots if is declare them globally. Any idea? – johny bravo Jul 23 '19 at 09:58
  • @johnybravo Use instead template argument of the list like std::function. – Vlad from Moscow Jul 23 '19 at 10:01
  • Im still learning, could you give some idea? Although not perfect but one workaround as i see, if i declare the member function static it works. Since i will have only one instance of the class doing this, in worse case i will just use this technique then – johny bravo Jul 23 '19 at 10:12
  • @johnybravo As I already mentioned you can use std;:function. – Vlad from Moscow Jul 23 '19 at 10:17