PDA

View Full Version : can a single statement work in signal/slot mechanism



salmanmanekia
8th August 2008, 07:37
hi..
i was just guessing that if there is any mechanism available in qt through which single signal can emit different slots just with a single statement..:rolleyes:


connect(navigate,SIGNAL(recordClicked()),navigate, SLOT(disableRecordButton()));
connect(navigate,SIGNAL(recordClicked()),progress, SLOT(startProgress()));

can this work be done by single statement

wysota
8th August 2008, 08:24
Well... Yes, if you implement a function such as the following:


QList<bool> multiconnect(QObject *sender, const char *sign, const QList<QObject*> &rcvs, const QList<const char*> &slts){
if(rcvs.size()!=slts.size())
return QList<bool>();
QList<bool> results;
for(int i=0;i<rcvs.size();i++){
results << connect(sender, sign, rcvs[i], slts[i]);
}
return results;
}

Then you should be able to call it as:

QList<QObject *> rcvs;
QList<const char *> slts;
rcvs << myObj1 << myObj2 << myObj3;
slts << SLOT(react()) << SLOT(doSomething()) << SLOT(doSomethingElse());
QList<bool> result = multiconnect(this, SIGNAL(clicked()), rcvs, slts);
if(result.isEmpty())
qDebug() << "Bad args count";
else if(result[2]==false)
qDebug() << "SLOT myObj3::doSomethingElse() didn't connect";

Of course this can be shortened to a single statement:

multiconnect(this, SIGNAL(clicked()), QList<QObject*>() << myObj1 << myObj2, QList<const char*>() << SLOT("xyz()") << SLOT("abc"));