![]() |
Home · Overviews · Examples | ![]() |
The QFtp class provides an implementation of the client side of FTP protocol. More...
Inherits QObject.
The QFtp class provides an implementation of the client side of FTP protocol.
The class works asynchronously, so there are no blocking functions. If an operation cannot be executed immediately, the function will still return straight away and the operation will be scheduled for later execution. The results of scheduled operations are reported via signals. This approach depends on the event loop being in operation.
The operations that can be scheduled (they are called "commands" in the rest of the documentation) are the following: connectToHost, login, close, list, cd, get, put, remove, mkdir, rmdir, rename and rawCommand.
All of these commands return a unique identifier that allows you to keep track of the command that is currently being executed. When the execution of a command starts, the commandStarted signal with the command's identifier is emitted. When the command is finished, the commandFinished signal is emitted with the command's identifier and a bool that indicates whether the command finished with an error.
In some cases, you might want to execute a sequence of commands, e.g. if you want to connect and login to a FTP server. This is simply achieved:
QFtp *ftp = new QFtp(parent); ftp->connectToHost("ftp.trolltech.com"); ftp->login();
In this case two FTP commands have been scheduled. When the last scheduled command has finished, a done signal is emitted with a bool argument that tells you whether the sequence finished with an error.
If an error occurs during the execution of one of the commands in a sequence of commands, all the pending commands (i.e. scheduled, but not yet executed commands) are cleared and no signals are emitted for them.
Some commands, e.g. list, emit additional signals to report their results.
Example: If you want to download the INSTALL file from Trolltech's FTP server, you would write this:
ftp->connectToHost("ftp.trolltech.com"); // id == 1 ftp->login(); // id == 2 ftp->cd("qt"); // id == 3 ftp->get("INSTALL"); // id == 4 ftp->close(); // id == 5
For this example the following sequence of signals is emitted (with small variations, depending on network traffic, etc.):
start(1) stateChanged(HostLookup) stateChanged(Connecting) stateChanged(Connected) finished(1, false) start(2) stateChanged(LoggedIn) finished(2, false) start(3) finished(3, false) start(4) dataTransferProgress(0, 3798) dataTransferProgress(2896, 3798) readyRead() dataTransferProgress(3798, 3798) readyRead() finished(4, false) start(5) stateChanged(Closing) stateChanged(Unconnected) finished(5, false) done(false)
The dataTransferProgress signal in the above example is useful if you want to show a progress bar to inform the user about the progress of the download. The readyRead signal tells you that there is data ready to be read. The amount of data can be queried then with the bytesAvailable function and it can be read with the read() or readAll function.
If the login fails for the above example, the signals would look like this:
start(1) stateChanged(HostLookup) stateChanged(Connecting) stateChanged(Connected) finished(1, false) start(2) finished(2, true) done(true)
You can then get details about the error with the error and errorString functions.
For file transfer, QFtp can use both active or passive mode, and it uses passive file transfer mode by default; see the documentation for setTransferMode for more details about this.
Call setProxy to make QFtp connect via an FTP proxy server.
The functions currentId and currentCommand provide more information about the currently executing command.
The functions hasPendingCommands and clearPendingCommands allow you to query and clear the list of pending commands.
If you are an experienced network programmer and want to have complete control you can use rawCommand to execute arbitrary FTP commands.
Warning: The current version of QFtp doesn't fully support non-Unix FTP servers. We hope to fix this in a future version of Qt.
See also QHttp and FTP Example.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.3.4_01 |