(SOLVED) Qt 5 and QFtp (qtftp)



  • Hello.

    I need to work with QFtp because I want a list of files from a ftp, and QNetworkAccessManager don't have this function implemented.

    I installed the QFtp but I can't connect, the code is:

    @
    QFtp ftp = new QFtp();

    connect(ftp, SIGNAL(stateChanged(int)), this, SLOT(ftp_state_change_slot(int)));

    ftp->connectToHost(QString("ftp://localhost"),21);

    // while(terrama_ftp->state() != QFtp::HostLookup);
    @

    I know the execution of QFtp is asynchronous and I wrote a slot, but the slot is never called, and even when I tried wait with a while it don't change his state.

    Thanks for any help!



  • Probably not helpful but just a comment.

    I thought QFtp was removed from Qt5. I used it in some Qt4 projects and was looking at alternatives for Qt5 because I read this somewhere.



  • Hi,

    Are you sure that your signal-slot connection is OK? Why don't you check the returned value from connect().

    Are you running an event loop?



  • Hello, thanks for reply!

    The connectToHost() is returning 1, and I think this is just the command ID.

    I'm not running a event loop, should I?

    Thanks!


  • Moderators

    [quote author="ViniciusCampanha" date="1421668693"]The connectToHost() is returning 1, and I think this is just the command ID.[/quote]ckakman meant Line #3 in your original post.

    [quote author="ViniciusCampanha" date="1421668693"]I'm not running a event loop, should I?[/quote]Yes, you need an event loop. The event loop responds to your stateChanged signal and runs your slot.

    Also, you must not have any infinite loops. Infinite loops block your slots from running.



  • Thanks! It works!

    So, I put a QCoreApplication in my main:

    @QCoreApplication a(argc, argv);@

    And changed the code to connect:
    @
    QFtp ftp = new QFtp();

    QEventLoop loop;
    connect(ftp, SIGNAL(done(bool)), &loop, SLOT(quit()));

    connect(ftp, SIGNAL(stateChanged(int)), this, SLOT(ftp_state_change_slot(int)));

    ftp->connectToHost(QString("localhost"),21);

    loop.exec();
    @

    I discovered that in connectToHost I can't put host like "ftp://localhost", it must to be just "localhost".

    One last doubt, if in another part of code I want to work with ftp again, I will need another QEventLoop ?

    Thanks!


  • Moderators

    [quote author="ViniciusCampanha" date="1421930032"]One last doubt, if in another part of code I want to work with ftp again, I will need another QEventLoop ?[/quote]

    No, one even loop is enough. In your snippet above you are running 1 loop and creating another: QCoreApplication has a built-in loop that is started when you call exec(). So in fact you do not need QEventLoop, it should be enough to just run a.exec() instead.



  • It works! Thanks!

    The only way I could make it work was putting one QEventLoop at each time I need to pass a command to QFtp and wait for signal done().

    It is because I dont know all the commands to pass at one time to QFtp, my program need to list the files at the ftp and then it will decide what to do with every file.

    Thanks again!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.