I'm learning about using QFtp.
I'd like to connect to a remote ftp server and list its content.
This what I wrote so far:
// libftp.cpp
#include "libftp.h"
libFTP::libFTP(QObject *parent) :
QObject(parent)
{
}
void libFTP::open(QString host)
{
connect(&ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(status(int,bool)));
connect(&ftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(dir(QUrlInfo)));
ftp.setTransferMode(QFtp::Active);
ftp.connectToHost(host);
ftp.list();
ftp.cd("USER");
}
void libFTP::disconnect()
{
ftp.abort();
ftp.deleteLater();
}
void libFTP::download(QString filename)
{
ftp.get(filename);
}
void libFTP::upload(QString path,QString filename)
{
QString fullpath=path+filename;
try
{
QFile *f= new QFile(fullpath);
if(f->exists())
{
qDebug()<<"File Trovato";
ftp.mkdir("test");
//ftp.put(f,filename);
f->close();
f->deleteLater();
}
}
catch(std::exception x)
{
qDebug()<<"Errore " << x.what();
}
}
void libFTP::status(int id,bool error)
{
if(error)
{
qDebug()<<"Errore";
}
else
qDebug()<<"Status ID " << QString(id);
}
void libFTP::dir(QUrlInfo directory)
{
qDebug()<<directory.name();
}
// libftp.h
#ifndef LIBFTP_H
#define LIBFTP_H
#include <QObject>
#include <QtCore>
#include <QFtp>
#include <QUrlInfo>
class libFTP : public QObject
{
Q_OBJECT
public:
explicit libFTP(QObject *parent = 0);
void open(QString host);
void disconnect();
void download(QString filename);
void upload(QString path,QString filename);
private:
QFtp ftp;
signals:
public slots:
void status(int id,bool error);
void dir(QUrlInfo directory);
};
#endif // LIBFTP_H
And I call it from main:
#include <libftp.h>
int main()
{
libFTP *ftp = new libFTP();
ftp->open("10.20.xx.xxx");
ftp->deleteLater();
}
The server I'm connecting to accepts anonymous login. When I try to debug this code, I notice that no slots are called and I don't see any FTP packets in my wireshark capture. In every example code I saw that's the way QFtp is used, what am I missing?
Found the issue, that wasn't immediate for a qt newbie, it didn't show any info. I had to run QFtp in a QApplication. I think this is needed by the event loop
calling QApplication in main() solved it.