PDA

View Full Version : Signal / Slot with specific argument and threads



tpf80
14th September 2007, 15:49
Have created a slot like shown below, in order to do something when a worker thread is done, but it must know which one finished:


void ProgramMain::serverDisconnectDone(int thread) {

}


Since I know what signal comes from what thread, I wanted to connect them in this way:


connect(&thread1, SIGNAL(logoffDone()), this, SLOT(serverDisconnectDone(1)));
connect(&thread2, SIGNAL(logoffDone()), this, SLOT(serverDisconnectDone(2)));
connect(&thread3, SIGNAL(logoffDone()), this, SLOT(serverDisconnectDone(3)));
connect(&thread4, SIGNAL(logoffDone()), this, SLOT(serverDisconnectDone(4)));


When I execute my program I get the error:


Object::connect: No such slot ProgramMain::serverDisconnectDone(1)
Object::connect: (receiver name: 'ProgramMain')
Object::connect: No such slot ProgramMain::serverDisconnectDone(2)
Object::connect: (receiver name: 'ProgramMain')
Object::connect: No such slot ProgramMain::serverDisconnectDone(3)
Object::connect: (receiver name: 'ProgramMain')
Object::connect: No such slot ProgramMain::serverDisconnectDone(4)
Object::connect: (receiver name: 'ProgramMain')


I know that there are more complicated ways that I could make this work, but the easiest way is to just force the argument in the signal slot connection. Is there any way that I can do this?

wysota
14th September 2007, 15:52
Take a look at QSignalMapper.

tpf80
14th September 2007, 22:55
Nice! at least by reading the description for it, this looks like its perfect for what I need. I will see if I can impliment it right away.

tpf80
14th September 2007, 23:43
Yes this worked perfectly! Here is the solution I used in case anyone later wants to know about it:


QSignalMapper* mapper = new QSignalMapper(this);

mapper->setMapping(&thread1, 1);
mapper->setMapping(&thread2, 2);
mapper->setMapping(&thread3, 3);
mapper->setMapping(&thread4, 4);

connect(&thread1, SIGNAL(logoffDone()), mapper, SLOT(map()));
connect(&thread2, SIGNAL(logoffDone()), mapper, SLOT(map()));
connect(&thread3, SIGNAL(logoffDone()), mapper, SLOT(map()));
connect(&thread4, SIGNAL(logoffDone()), mapper, SLOT(map()));

connect(mapper, SIGNAL(mapped(int)), this, SLOT(serverDisconnectDone(int)));



Thanks again for pointing me in the right direction!