Qt中的QFtp类

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();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值