ftpclient-example.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 937 行 · 第 1/3 页
HTML
937 行
progressLabel1 = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "No Operation in Progress" ), mainWidget ); progressBar1 = new <a href="qprogressbar.html">QProgressBar</a>( mainWidget ); progressLabel2 = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "No Operation in Progress" ), mainWidget ); progressBar2 = new <a href="qprogressbar.html">QProgressBar</a>( mainWidget );<a name="x684"></a> progressLabel1-><a href="qwidget.html#hide">hide</a>(); progressBar1-><a href="qwidget.html#hide">hide</a>(); progressLabel2-><a href="qwidget.html#hide">hide</a>(); progressBar2-><a href="qwidget.html#hide">hide</a>(); <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( mainWidget );}void <a name="f267"></a>FtpMainWindow::slotLocalDirChanged( const <a href="qstring.html">QString</a> &path ){ // The user changed the path on the left side oldLocal = localOperator; localOperator.setPath( path ); localOperator.listChildren();}void FtpMainWindow::slotLocalDirChanged( const <a href="qurlinfo.html">QUrlInfo</a> &info ){ // The user changed the path on the left side oldLocal = localOperator;<a name="x680"></a> localOperator.addPath( info.<a href="qurlinfo.html#name">name</a>() ); localOperator.listChildren(); localCombo-><a href="qcombobox.html#insertItem">insertItem</a>( localOperator.path(), 0 );<a name="x653"></a> localCombo-><a href="qcombobox.html#setCurrentItem">setCurrentItem</a>( 0 );}void <a name="f268"></a>FtpMainWindow::slotRemoteDirChanged( const <a href="qstring.html">QString</a> &path ){ // The user changed the path on the right side if ( !remoteOperator.isValid() ) return; oldRemote = remoteOperator; remoteOperator.setPath( path ); remoteOperator.listChildren();}void FtpMainWindow::slotRemoteDirChanged( const <a href="qurlinfo.html">QUrlInfo</a> &info ){ // The user changed the path on the right side oldRemote = remoteOperator; remoteOperator.addPath( info.<a href="qurlinfo.html#name">name</a>() ); remoteOperator.listChildren(); remotePathCombo-><a href="qcombobox.html#insertItem">insertItem</a>( remoteOperator.path(), 0 ); remotePathCombo-><a href="qcombobox.html#setCurrentItem">setCurrentItem</a>( 0 );}void <a name="f269"></a>FtpMainWindow::slotConnect(){ // The user pressed the connect button, so let's connect to the // FTP server // First we need to set stuff (host, path, etc.) which the user // entered on the right side to the remote QUrlOperator // protocol + hostname<a name="x651"></a> <a href="qstring.html">QString</a> s = "ftp://" + remoteHostCombo-><a href="qcombobox.html#currentText">currentText</a>(); oldRemote = remoteOperator; remoteOperator = s; // path on the server if ( !remotePathCombo-><a href="qcombobox.html#currentText">currentText</a>().isEmpty() ) remoteOperator.setPath( remotePathCombo-><a href="qcombobox.html#currentText">currentText</a>() ); else remoteOperator.setPath( "/" ); // if nothing or "ftp" or "anonymous" has been entered into the username combo, // let's connect anonymous, else private with password if ( !userCombo-><a href="qcombobox.html#currentText">currentText</a>().isEmpty() && userCombo-><a href="qcombobox.html#currentText">currentText</a>().lower() != "anonymous" && userCombo-><a href="qcombobox.html#currentText">currentText</a>().lower() != "ftp" ) { remoteOperator.setUser( userCombo-><a href="qcombobox.html#currentText">currentText</a>() );<a name="x662"></a> remoteOperator.setPassword( passLined-><a href="qlineedit.html#text">text</a>() ); } // set the port remoteOperator.setPort( portSpin-><a href="qspinbox.html#value">value</a>() ); // finally read the directory on the ftp server remoteOperator.listChildren();}void <a name="f270"></a>FtpMainWindow::slotUpload(){ // the user pressed the upload button // if files have been selected on the left side (local filesystem) <a href="qvaluelist.html">QValueList</a><QUrlInfo> files = leftView->selectedItems();<a name="x683"></a> if ( files.<a href="qvaluelist.html#isEmpty">isEmpty</a>() ) return; // create a list of the URLs which should be copied <a href="qstringlist.html">QStringList</a> lst; QValueList<QUrlInfo>::Iterator it = files.<a href="qvaluelist.html#begin">begin</a>(); for ( ; it != files.<a href="qvaluelist.html#end">end</a>(); ++it ) lst << QUrl( localOperator, ( *it ).name() ); // copy the list of selected files to the directory in which the // remoteOperator currently is (upload) remoteOperator.copy( lst, remoteOperator, FALSE );}void <a name="f271"></a>FtpMainWindow::slotDownload(){ // if the user pressed the download button // if files have been selected on the right side (remote filesystem) <a href="qvaluelist.html">QValueList</a><QUrlInfo> files = rightView->selectedItems(); if ( files.<a href="qvaluelist.html#isEmpty">isEmpty</a>() ) return; // create a list of the URLs which should be downloaded <a href="qstringlist.html">QStringList</a> lst; QValueList<QUrlInfo>::Iterator it = files.<a href="qvaluelist.html#begin">begin</a>(); for ( ; it != files.<a href="qvaluelist.html#end">end</a>(); ++it ) lst << QUrl( remoteOperator, ( *it ).name() ); // copy the list of selected files to the directory in which the // localOperator currently is (download) localOperator.copy( lst, localOperator, FALSE );}void <a name="f272"></a>FtpMainWindow::slotLocalStart( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ){ // this slot is always called if the local QUrlOperator starts // listing a directory or dowloading a file if ( !op ) return;<a name="x667"></a> if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpListChildren ) { // start listing a dir? clear the left view!<a name="x663"></a> leftView-><a href="qlistview.html#clear">clear</a>(); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) { // start downloading a file? reset the progress bar!<a name="x672"></a> progressBar1-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 0 );<a name="x670"></a> progressBar1-><a href="qprogressbar.html#reset">reset</a>(); }}void <a name="f273"></a>FtpMainWindow::slotLocalFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ){ // this slot is always called if the local QUrlOperator finished // an operation if ( !op ) return;<a name="x669"></a> if ( op && op-><a href="qnetworkoperation.html#state">state</a>() == QNetworkProtocol::StFailed ) { // an error happend, let the user know that<a name="x668"></a><a name="x664"></a> QMessageBox::<a href="qmessagebox.html#critical">critical</a>( this, tr( "ERROR" ), op-><a href="qnetworkoperation.html#protocolDetail">protocolDetail</a>() ); // do something depending in the error code<a name="x666"></a> int ecode = op-><a href="qnetworkoperation.html#errorCode">errorCode</a>(); if ( ecode == QNetworkProtocol::ErrListChildren || ecode == QNetworkProtocol::ErrParse || ecode == QNetworkProtocol::ErrUnknownProtocol || ecode == QNetworkProtocol::ErrLoginIncorrect || ecode == QNetworkProtocol::ErrValid || ecode == QNetworkProtocol::ErrHostNotFound || ecode == QNetworkProtocol::ErrFileNotExisting ) { localOperator = oldLocal;<a name="x654"></a> localCombo-><a href="qcombobox.html#setEditText">setEditText</a>( localOperator.path() ); localOperator.listChildren(); } } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpPut ) { // finished saving the downloaded file? reread the dir and hide the progress bar localOperator.listChildren(); progressLabel1-><a href="qwidget.html#hide">hide</a>(); progressBar1-><a href="qwidget.html#hide">hide</a>(); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) { // finished reading a file from the ftp server? reset the progress bar progressBar1-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 0 ); progressBar1-><a href="qprogressbar.html#reset">reset</a>(); }}void <a name="f274"></a>FtpMainWindow::slotRemoteStart( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ){ // this slot is always called if the remote QUrlOperator starts // listing a directory or uploading a file if ( !op ) return; if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpListChildren ) { // start listing a dir? clear the right view! rightView-><a href="qlistview.html#clear">clear</a>(); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) { // start downloading a file? reset the progress bar! progressBar2-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 0 ); progressBar2-><a href="qprogressbar.html#reset">reset</a>(); }}void <a name="f275"></a>FtpMainWindow::slotRemoteFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ){ // this slot is always called if the remote QUrlOperator finished // an operation if ( !op ) return; if ( op && op-><a href="qnetworkoperation.html#state">state</a>() == QNetworkProtocol::StFailed ) { // an error happend, let the user know that QMessageBox::<a href="qmessagebox.html#critical">critical</a>( this, tr( "ERROR" ), op-><a href="qnetworkoperation.html#protocolDetail">protocolDetail</a>() ); // do something depending in the error code int ecode = op-><a href="qnetworkoperation.html#errorCode">errorCode</a>(); if ( ecode == QNetworkProtocol::ErrListChildren || ecode == QNetworkProtocol::ErrParse || ecode == QNetworkProtocol::ErrUnknownProtocol || ecode == QNetworkProtocol::ErrLoginIncorrect || ecode == QNetworkProtocol::ErrValid || ecode == QNetworkProtocol::ErrHostNotFound || ecode == QNetworkProtocol::ErrFileNotExisting ) { remoteOperator = oldRemote; remoteHostCombo-><a href="qcombobox.html#setEditText">setEditText</a>( remoteOperator.host() ); remotePathCombo-><a href="qcombobox.html#setEditText">setEditText</a>( remoteOperator.path() );<a name="x661"></a> passLined-><a href="qlineedit.html#setText">setText</a>( remoteOperator.password() ); userCombo-><a href="qcombobox.html#setEditText">setEditText</a>( remoteOperator.user() ); portSpin-><a href="qspinbox.html#setValue">setValue</a>( remoteOperator.port() ); remoteOperator.listChildren(); } } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpListChildren ) { // finished reading a dir? set the correct path to the pth combo of the right view remotePathCombo-><a href="qcombobox.html#setEditText">setEditText</a>( remoteOperator.path() ); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpPut ) { // finished saving the uploaded file? reread the dir and hide the progress bar remoteOperator.listChildren(); progressLabel2-><a href="qwidget.html#hide">hide</a>(); progressBar2-><a href="qwidget.html#hide">hide</a>(); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) { // finished reading a file from the local filesystem? reset the progress bar progressBar2-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 0 ); progressBar2-><a href="qprogressbar.html#reset">reset</a>(); }}void <a name="f276"></a>FtpMainWindow::slotLocalDataTransferProgress( int bytesDone, int bytesTotal, <a href="qnetworkoperation.html">QNetworkOperation</a> *op ){ // Show the progress here of the local QUrlOperator reads or writes data if ( !op ) return;<a name="x685"></a> if ( !progressBar1-><a href="qwidget.html#isVisible">isVisible</a>() ) { if ( bytesDone < bytesTotal) { progressLabel1-><a href="qwidget.html#show">show</a>();<a name="x673"></a> progressBar1-><a href="qwidget.html#show">show</a>(); progressBar1-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( bytesTotal );<a name="x671"></a> progressBar1-><a href="qprogressbar.html#setProgress">setProgress</a>( 0 ); progressBar1-><a href="qprogressbar.html#reset">reset</a>(); } else return; }<a name="x674"></a> if ( progressBar1-><a href="qprogressbar.html#totalSteps">totalSteps</a>() == bytesTotal ) progressBar1-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( bytesTotal ); if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet )<a name="x665"></a><a name="x658"></a> progressLabel1-><a href="qlabel.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "Read: %1" ).arg( op-><a href="qnetworkoperation.html#arg">arg</a>( 0 ) ) ); else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpPut ) progressLabel1-><a href="qlabel.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "Write: %1" ).arg( op-><a href="qnetworkoperation.html#arg">arg</a>( 0 ) ) ); else return; progressBar1-><a href="qprogressbar.html#setProgress">setProgress</a>( bytesDone );}void <a name="f277"></a>FtpMainWindow::slotRemoteDataTransferProgress( int bytesDone, int bytesTotal, <a href="qnetworkoperation.html">QNetworkOperation</a> *op ){ // Show the progress here of the remote QUrlOperator reads or writes data if ( !op ) return; if ( !progressBar2-><a href="qwidget.html#isVisible">isVisible</a>() ) { if ( bytesDone < bytesTotal) { progressLabel2-><a href="qwidget.html#show">show</a>(); progressBar2-><a href="qwidget.html#show">show</a>(); progressBar2-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( bytesTotal ); progressBar2-><a href="qprogressbar.html#setProgress">setProgress</a>( 0 ); progressBar2-><a href="qprogressbar.html#reset">reset</a>(); } else return; } if ( progressBar2-><a href="qprogressbar.html#totalSteps">totalSteps</a>() != bytesTotal ) progressBar2-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( bytesTotal ); if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) progressLabel2-><a href="qlabel.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "Read: %1" ).arg( op-><a href="qnetworkoperation.html#arg">arg</a>( 0 ) ) ); else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpPut ) progressLabel2-><a href="qlabel.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "Write: %1" ).arg( op-><a href="qnetworkoperation.html#arg">arg</a>( 0 ) ) ); else return; progressBar2-><a href="qprogressbar.html#setProgress">setProgress</a>( bytesDone );}void <a name="f278"></a>FtpMainWindow::slotLocalMkDir(){ // create a dir on the local filesystem bool ok = FALSE;<a name="x657"></a> <a href="qstring.html">QString</a> name = QInputDialog::<a href="qinputdialog.html#getText">getText</a>( <a href="qobject.html#tr">tr</a>( "Directory Name:" ), <a href="qstring.html#QString-null">QString::null</a>, QLineEdit::Normal, QString::null, &ok, this );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?