Question

I feel like I'm probably missing something very easy here, but I'm at a loss to figure out what. I have a C++ function (with Qt 4.7) where I need to access the files on an FTP server. To do this, I have the following set up:

QString source = "ftp://username:password@ftp.myftpserver.com/directoryname/";
QFtp *ftp = new QFtp(this);
ftp->connectToHost(source);

connect(ftp, SIGNAL(listInfo(QUrlInfo)), this, SLOT(processInfoFromFile(QUrlInfo)));
connect(ftp, SIGNAL(done(bool)), this, SLOT(finishThisProcess()));

ftp->list();

When I type the source directly into a browser it comes up correctly and shows me all the files inside the directory. I also have another QFtp instance (different variable names) elsewhere in the program set up the same way; that works. However, with this one it simply interprets the directory at source as being empty and immediately jumps to finishThisProcess. Is there something I'm missing? Thanks!

EDIT: this is the other instance of the ftp client:

ftp2 = new QFtp(this);
QString user = "username";
QString pass = "password";

connect(ftp2, SIGNAL(listInfo(QUrlInfo)), this, SLOT(processInfoFromFile(QUrlInfo)));
connect(ftp2, SIGNAL(done(bool)), this, SLOT(finishThisProcess()));

ftp2->connectToHost("ftp.myftpserver.com");
ftp2->login(user, pass);
ftp2->list();

It's the same as the other except a)this one tries to access one directory level farther up, and b)I declared the username and password separately and then login manually. I tried the one giving me problems this way, but to no avail.

Was it helpful?

Solution

1) You should connect the signals and slots before the relevant statements.

2) Also, you should use the login method with the username and password.

So, your code should look like this:

QString source = "ftp://ftp.myftpserver.com/directoryname/";
QFtp *ftp = new QFtp(this);

connect(ftp, SIGNAL(listInfo(QUrlInfo)), this, SLOT(processInfoFromFile(QUrlInfo)));
connect(ftp, SIGNAL(done(bool)), this, SLOT(finishThisProcess()));

ftp->connectToHost(source);
ftp->login(username, password);
ftp->list();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top