Qt Network (4) FTP (2)

Source: Internet
Author: User
Tags ftp client

This article was originally reproduced at www.yafeilinux.com. Please indicate the source.

The previous section describes how to write the simplest FTP client program. In this section, we extend this program so that it can browse and download all the files on the server.

1.Modify widget. UIThe file is as follows.

We deleted text browser and added several labels, line edit, push button components, a tree widget, And a progress bar component. Then we make the following changes to several of the parts.

(1) Change the objectname attribute of line edit after the "FTP server" label to "ftpserverlineedit", and change the text attribute to "ftp.qt.nokia.com ".

(2) Change the objectname attribute of line edit after the "user name" label to "usernamelineedit", change its text attribute to "anonymous", and change its tooltip attribute to "use the default user name: anonymous, the password is arbitrary."

(3) change the objectname attribute of line edit after the "password" label to "passwordlineedit", change its text attribute to "123456", and change its echomode attribute to "password ".

(4) change the objectname attribute of the "Connect" button to "connectbutton ".

(5) change the objectname attribute of the "back to the upper-level directory" button to "cdtoparentbutton ".

(6) change the objectname attribute of the "Download" button to "downloadbutton ".

(7) change the objectname attribute of the tree widget to "filelist", right-click the tree widget, select the edit items menu, and add the following column attributes.

 

The final interface is as follows.

 

In our program, after the user fills in the relevant information, press the "Connect" button to connect to the FTP server and display all the files on the server in the tree widget, you can click the "Download" button to download the selected file and use the progress bar to display the download progress.

2.Modify widget. hFile.

(1) Add a header file # include <qtgui>

(2) Add a variable to private:

Qhash <qstring, bool> isdirectory;

// Used to store information about whether a path is a directory

Qstring currentpath;

// Used to store the current path

(3) Add a slot function:

Private slots:

Void on_downloadbutton_clicked ();

Void on_cdtoparentbutton_clicked ();

Void on_connectbutton_clicked ();

Void ftpcommandfinished (INT, bool );

Void ftpcommandstarted (INT );

Void updatedatatransferprogress (qint64, qint64 );

// Update the progress bar

Void addtolist (const qurlinfo & urlinfo );

// Add files on the server to the tree widget

Void processitem (qtreewidgetitem *, INT );

// Double-click a directory to display its content

3.Modify widget. cpp.

(1) Click the event slot function to implement the "Connect" button.

Void Widget: on_connectbutton_clicked () // connection button

{

UI-> filelist-> clear ();

Currentpath. Clear ();

Isdirectory. Clear ();

FTP = new qftp (this );

Connect (FTP, signal (commandstarted (INT), this, slot (ftpcommandstarted (INT )));

Connect (FTP, signal (commandfinished (INT, bool )),

This, slot (ftpcommandfinished (INT, bool )));

Connect (FTP, signal (listinfo (qurlinfo), this, slot (addtolist (qurlinfo )));

Connect (FTP, signal (datatransferprogress (qint64, qint64 )),

This, slot (updatedatatransferprogress (qint64, qint64 )));

Qstring ftpserver = UI-> ftpserverlineedit-> text ();

Qstring username = UI-> usernamelineedit-> text ();

Qstring Password = UI-> passwordlineedit-> text ();

FTP-> connecttohost (ftpserver, 21); // connect to the server. The default port number is 21.

FTP-> login (username, password); // log on

}

In the "Connect" button, click the event slot function to create an FTP object and associate the related signals and slots. The listinfo () signal is sent by the ftp-> List () function. It will be called when the logon command is complete. We will refer to it below. The datatransferprogress () signal is automatically transmitted during data transmission. Finally, we obtain the server address, user name, password, and other information from the interface, and execute the connection and login commands with them as parameters.

(2) Change the ftpcommandfinished () function.

Make changes in the corresponding location.

First, when the logon command is complete, we call the list () function:

UI-> label-> settext (TR ("Logon successful "));

FTP-> List (); // transmits the listinfo () signal to display the file list

Then, when the download command is complete, we make the Download button available:

UI-> label-> settext (TR ("downloaded "));

UI-> downloadbutton-> setenabled (true );

Finally, add an if statement to process the status when the LIST command is complete:

If (ftp-> currentcommand () = qftp: List ){

If (isdirectory. isempty ())

{// If the directory is empty, "empty" is displayed"

UI-> filelist-> addtoplevelitem (

New qtreewidgetitem (qstringlist () <tr ("<empty> ")));

UI-> filelist-> setenabled (false );

UI-> label-> settext (TR ("this directory is empty "));

}

}

When the LIST command is complete, determine whether the file list is empty. If it is empty, make the tree widget unavailable and display the "empty" entry.

(3) Add the file list function as follows.

Void Widget: addtolist (const qurlinfo & urlinfo) // Add a file list

{

Qtreewidgetitem * Item = new qtreewidgetitem;

Item-> settext (0, urlinfo. Name ());

Item-> settext (1, qstring: Number (urlinfo. Size ()));

Item-> settext (2, urlinfo. Owner ());

Item-> settext (3, urlinfo. Group ());

Item-> settext (4, urlinfo. lastmodified (). tostring ("Mmm dd YYYY "));

Qpixmap pixmap (urlinfo. isdir ()? "../Dir.png": "../file.png ");

Item-> seticon (0, pixmap );

Isdirectory [urlinfo. Name ()] = urlinfo. isdir ();

// Store the directory information for this path

UI-> filelist-> addtoplevelitem (item );

If (! UI-> filelist-> currentitem ()){

UI-> filelist-> setcurrentitem (ui-> filelist-> toplevelitem (0 ));

UI-> filelist-> setenabled (true );

}

}

When the ftp-> List () function is executed, the listinfo () signal is sent, and the addtolist () function is executed. The file information is displayed on the tree widget, and store the path of the file and whether the file is a directory in isdirectory. To partition files and directories, we use different icons file.png and dir.png to represent them. These two icons are placed in the project folder.

(4) change the constructor content as follows.

{

UI-> setupui (this );

UI-> progressbar-> setvalue (0 );

Connect (ui-> filelist, signal (itemactivated (qtreewidgetitem *, INT )),

This, slot (processitem (qtreewidgetitem *, INT )));

// Double-click the directory in the list.

}

Here we only set the progress bar value to 0, and then associate the itemactivated () signal of the tree widget (). When you double-click an entry, this signal is sent. in the slot function, we can determine whether the entry is a directory. If so, the entry enters the directory.

(5) Implement the processitem () function as follows.

Void Widget: processitem (qtreewidgetitem * item, INT) // open a directory

{

Qstring name = item-> text (0 );

If (isdirectory. Value (name) {// if this file is a directory, open

UI-> filelist-> clear ();

Isdirectory. Clear ();

Currentpath + = '/';

Currentpath + = Name;

FTP-> Cd (name );

FTP-> List ();

UI-> cdtoparentbutton-> setenabled (true );

}

}

(6) The Click Event slot function of the "back to the upper-level directory" button is as follows.

Void Widget: on_cdtoparentbutton_clicked () // return the upper-level directory button

{

UI-> filelist-> clear ();

Isdirectory. Clear ();

Currentpath = currentpath. Left (currentpath. lastindexof ('/'));

If (currentpath. isempty ()){

UI-> cdtoparentbutton-> setenabled (false );

FTP-> Cd ("/");

} Else {

FTP-> Cd (currentpath );

}

FTP-> List ();

}

When returning the upper-level directory, we take the part before the last "/" of the current path. If the path is empty, we will make the "back to the upper-level directory" button unavailable.

(7) Click the "Download" button and the event slot function is as follows.

Void Widget: on_downloadbutton_clicked () // Download button

{

Qstring filename = UI-> filelist-> currentitem ()-> text (0 );

Qfile * file = new qfile (filename );

If (! File-> open (qiodevice: writeonly ))

{

Delete file;

Return;

}

UI-> downloadbutton-> setenabled (false); // The Download button is not available until the download is complete.

FTP-> get (ui-> filelist-> currentitem ()-> text (0), file );

}

Here we get the file name of the current project, create a new file, and use the get () command to download the file on the server to the new file.

(8) Update the progress bar function as follows.

Void Widget: updatedatatransferprogress (// progress bar

Qint64 readbytes, qint64 totalbytes)

{

UI-> progressbar-> setmaximum (totalbytes );

UI-> progressbar-> setvalue (readbytes );

}

 

 

 

 

 

4.Run the program as follows.

The START interface is as follows.

 

The logon interface is as follows.

 

The interface for downloading files is as follows.

 

When a directory is empty, the interface is as follows.

 

4.Process description.

The process of the entire program is the same as the order in which we implement functions. The user enters the server information on the interface. Then, we use the information to connect to and log on to the server. When the server is successfully logged on, we will list all the files on the server. For a directory, We can enter it and return to the upper-level directory. We can download the file and display the download progress.

All FTP operations are performed by the commands and numbers. You only need to call the corresponding commands and process them when sending signals. The display of files is the knowledge of the view.

5.Other instructions.

It should be noted that, in order to better explain the knowledge and simplify the program, we save a lot of detail processing. If necessary, you can add it by yourself. For example, you can use

FTP-> abort () function. You can also refer to the FTP example provided by QT. For other operations, such as upload, you can add them as needed.

FTP-related programming is mentioned here.

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.