QT中可以使用FTP进行FTP传输服务, 主要有以下几个函数,连接主机函数connectToHost(),登陆函数login(qstring username,qstring password)其中username为用户名,password为登陆口令,cd()打开所要传输的文件,get()函数是开始向本地文件传输,close()是关闭FTP连接。
头文件为
form1.h
/****************************************************************************
** Form interface generated from reading ui file 'form1.ui'
**
** Created: 三 1月 30 15:49:45 2008
** by: The User Interface Compiler ($Id: qt/main.cpp 3.1.1 edited Nov 21 17:40 $)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#ifndef FORM1_H
#define FORM1_H
#include <qvariant.h>
#include <qwidget.h>
#include <qftp.h>
#include <qmessagebox.h>
#include <qfile.h>
#include <qstring.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLineEdit;
class QPushButton;
class Form1 : public QWidget
{
Q_OBJECT
public:
Form1( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~Form1();
QLineEdit* lineEdit1;
QPushButton* pushButton1;
protected:
private:
QFtp ftp;
QFile file;
QString ss;
protected slots:
virtual void languageChange();
virtual void transfer();
};
#endif // FORM1_H
源文件为:
/****************************************************************************
** Form implementation generated from reading ui file 'form1.ui'
**
** Created: 三 1月 30 15:49:57 2008
** by: The User Interface Compiler ($Id: qt/main.cpp 3.1.1 edited Nov 21 17:40 $)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "form1.h"
#include <qvariant.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
/*
* Constructs a Form1 as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*/
Form1::Form1( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
if ( !name )
setName( "Form1" );
lineEdit1 = new QLineEdit( this, "lineEdit1" );
lineEdit1->setGeometry( QRect( 70, 100, 211, 71 ) );
pushButton1 = new QPushButton( this, "pushButton1" );
pushButton1->setGeometry( QRect( 110, 210, 101, 31 ) );
languageChange();
resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
// signals and slots connections
connect( pushButton1, SIGNAL( clicked() ), this, SLOT( adjustSize() ) );
connect( pushButton1,SIGNAL(clicked()),this,SLOT(transfer()));
}
/*
* Destroys the object and frees any allocated resources
*/
Form1::~Form1()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void Form1::languageChange()
{
setCaption( tr( "Form1" ) );
pushButton1->setText( tr( "pushButton1" ) );
}
void Form1::transfer()
{
ss=lineEdit1->text();
file.setName("transfer.data");
if(!file.open(IO_WriteOnly))
{
QMessageBox::warning(this,tr("transfer"),tr("cannot write the file"));
return;
}
ftp.connectToHost(ss);
ftp.login();
ftp.cd("/topsecret/csv");
ftp.get("transfer.data",&file);
ftp.close();
file.close();
}
mian函数为
#include <qapplication.h>
#include "form1.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
Form1 w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}