View Full Version : Synchronous QFtp?

21st October 2009, 12:27
Hello everybody,

my application checks at start (actually at the beginning of the MainWindow constructor) whether last run quit cleanly, otherwise asks the user if s/he wants to upload a log file to an ftp server. It does something very simple like this:

if ( crashed == true )
uploadLogs( );

where the function uploadLogs( ) takes care of uploading the log file using QFtp. Since QFtp works asynchronously, uploadLogs( ) returns immediately, while the whole ftp login, transfer and closing takes a few seconds. My question is whether is it possible to prevent uploadLogs( ) from returning until the last pending command has completed.

Thanks a lot for any help.

23rd October 2009, 16:29
Hi guys,

I found the answer. Here if somebody needs it as well:

QUrl url( "ftp://example.com" );
ftp->connectToHost( url.host( ), url.port( 21 ) );
ftp->login( "anonymous", "" );

// Put all the ftp commands you need

ftp->close( );

// Process the queue of ftp events
while ( ( ftp->hasPendingCommands( ) ) || ( ftp->currentCommand( ) != QFtp::None ) )
qApp->processEvents( );

26th October 2009, 10:28
Your code will use 100% CPU.
A better solution is to use a QEventLoop and connect the QFtp done() signal to the quit() of the QEventLoop. Then you can just start the QFtp and then run eventLoop.exec().