PDA

View Full Version : When's SLOT invoked ?



phtdce
19th November 2011, 05:06
My example:


void funcMain()
{
QFtp * ftp = new QFtp(this);
connect(ftp, SIGNAL(listInfo(QUrlInfo)),
this, SLOT(addToList(QUrlInfo)));
.....
ftp->list(); // emit signal listInfo();
.....
return;
}

void addToList(QUrlInfo info)
{
//do something
}


When debug, SLOT(addToList) is invoked after funcMain() return. But i want addToList() to be invoked right after statement ftp->list() finish and before funcMain return , so what should i do ? And signal listInfo() is emitted while invoking ftp->list() or after ftp->list() finish or after funcMain() return ?

grin
19th November 2011, 14:06
See:
http://doc.qt.nokia.com/stable/qobject.html#connect-2
http://doc.qt.nokia.com/stable/qt.html#ConnectionType-en

You need Qt::DirectConnection type, i.e.


connect(ftp, SIGNAL(listInfo(QUrlInfo)),
this, SLOT(addToList(QUrlInfo)),Qt::DirectConnection);

phtdce
19th November 2011, 16:47
DirectConnection is like AutoConnection

Lesiok
19th November 2011, 17:43
DirectConnection is like AutoConnectionOnly when sender and receiver are in this same thread.

ChrisW67
22nd November 2011, 08:57
QFtp::list() is run asynchronously. From the friendly manual:

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted() and commandFinished().
Nothing happens, i.e. no request is sent and hence no responses received, until your program returns to the event loop. You can redesign with asynchronous events in mind, use QCoreApplication::processEvents(), or use a local event loop (QEventLoop)