Analysis of the FTP network learning base of QT network

Source: Internet
Author: User
Tags constructor ftp ftp client file transfer protocol

The basis of the QT network's FTP network learning is the content to be introduced in this article, first of all possible content. FTP is the file Transfer Protocol, which is the document transfer Protocol. The primary role of FTP is to have users connect to a remote computer, see what files the remote computer has, and then copy the files from the remote computer to the local computer, or send the local computer's files to the remote computer.

In Qt, we can use the Qnetworkaccessmanager and qnetworkreply classes described in the previous section to write FTP programs because they are easy to use. However, for more complex FTP operations, QT also provides the Qftp class, which makes it easy to write an FTP client program with this class. Let's look at this class in help first.

In qftp, all operations correspond to a specific function, and we can call them commands. such as Connecttohost () Connect to server command, login () login command, get () Download command, mkdir () New directory command, etc. Because the Qftp class works asynchronously, all of these functions are not blocking functions. That is, if an operation cannot be executed immediately, then the function will return directly until the program control returns to the Qt event loop before it is actually executed, which does not affect the display of the interface.

All commands return a number of type int, which allows us to track this command and see its execution status. A commandstarted () signal is emitted when each command starts executing, and a commandfinished () signal is emitted when the command executes. We can use these two signals and the number of the command to get the execution state of the command. Of course, we do not want to write down the number of each command, so we can also use Currentcommand () to get the command that is now executed, and the corresponding relationship between the return value and the command is shown below.

Let's look at a simple example of an FTP client and then extend it.

In this example we download a file from the FTP server and display it.

1, we new QT4 Gui qapplication.

The project is named "Myftp", then the Qtnetwork module is selected, and the base class selects Qwidget.

2, modify the Widget.ui file.

Add a text Browser and a Label to it, and the effect is as follows.

3, in the main.cpp to modify.

In order to be able to use Chinese in the program, we add header file in Main.cpp # include <QTextCodec>

and add code in the main () function: Qtextcodec::setcodecfortr (Qtextcodec::codecforlocale ());

4, in the widget.h to modify.

Add header file First: #include <QFtp>

Then define the object in private: Qftp *ftp;

To add a private slot function:

Private slots:void ftpcommandstarted (int); void ftpcommandfinished (Int,bool);

5. Make changes in Widget.cpp.

(1) Add code to the constructor:

 
FTP = new Qftp (this); Ftp->connecttohost ("ftp.qt.nokia.com");   Connect to server Ftp->login ();   Login FTP-&GT;CD ("QT");  Jump to "QT" directory under Ftp->get ("INSTALL");   Download "INSTALL" file Ftp->close ();       Close connection Connect (ftp,signal (commandstarted (int)), This,slot (ftpcommandstarted (int))); When each command starts executing, the corresponding signal is sent to connect (ftp,signal (commandfinished (Int,bool)), This,slot (ftpcommandfinished (Int,bo   OL))); Signals when each command is executed at the end

We performed several FTP operations in the constructor, logged in to the site, and downloaded a file. We then correlated two signals and slots to track the execution of the command.

(2) Implement the slot function:

void widget::ftpcommandstarted (int)   {      if (Ftp->currentcommand ()   == qftp::connecttohost) {          ui->label->settext (tr ("Connecting to Server ...");       }       if  (Ftp->currentcommand ()  ==  Qftp::login) {          ui->label->settext (tr ("Logging in ..."));       }       if  (Ftp->currentcommand ()  ==  Qftp::get) {          ui->label->settext (tr ("Downloading ..."));       }       else if  (Ftp->currentcommand ()   == qftp::close) {          ui->label->settext (tr ("Closing connection ...”));       }  } 

Every time the command executes, it executes the ftpcommandstarted () function, which has a parameter int ID, which is the ID that is returned when the command is invoked, such as int loginID = Ftp->login (); At this point, we can use if (id = = LoginID) to determine whether the login () function is executed. However, we do not want to set a variable for each command to store its return value, so we use Ftp->currentcommand () here, which can also get the type of command currently executing. In this function we let different state information be displayed when different commands are started.

Void widget::ftpcommandfinished (Int,bool error)   {      if (ftp-> Currentcommand ()  == qftp::connecttohost) {          if ( Error)  ui->label->settext (TR ("Connection server errors:%1″)". Arg (Ftp->errorstring ()));           else ui->label->settext (TR ("Connect to Server Success"));       }       if  (Ftp->currentcommand ()  ==  Qftp::login) {          if (Error)  ui->label->settext (TR ( "Login error:%1″)." Arg (ftp->errorstring ()));           else ui->label->settext (TR ("login successful"));       }       if  (Ftp->currentcommand ()  ==  Qftp::get) {          if (Error)  ui->label->settext (tr("Download error:%1″)." Arg (ftp->errorstring ()));           else {      &

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.