QFtp 文件列表重复列出(已解决)
发表于 : 2013-02-09 17:41
列出服务器端文件时,重复列出,一直循环,如下图:
下载 ,参照 http://qt.csdn.net/articles.aspx?pointid=488&pointid2=7 写的。
程序代码:代码: 全选
diff -Nur mtFtp/mainwindow.cpp mtFtp-fixed/mainwindow.cpp
--- mtFtp/mainwindow.cpp 2013-02-10 01:02:37.000000000 +0800
+++ mtFtp-fixed/mainwindow.cpp 2013-02-12 13:39:11.210062268 +0800
@@ -2,17 +2,16 @@
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
+ QMainWindow(parent)
+ , ui(new Ui::MainWindow)
+ , ftp(0)
+ , file(0)
{
ui->setupUi(this);
ui->progressBar->setValue(0);
connect(ui->fileList, SIGNAL(itemActivated(QTreeWidgetItem*,int)),
this, SLOT(processItem(QTreeWidgetItem*,int)));
- connect(ui->connectButton, SIGNAL(pressed()), this, SLOT(on_connectButton_clicked()));
- connect(ui->downloadButton, SIGNAL(pressed()), this, SLOT(on_downloadButton_clicked()));
- connect(ui->cdToParentButton, SIGNAL(pressed()), this, SLOT(on_cdToParentButton_clicked()));
}
@@ -35,11 +34,6 @@
void MainWindow::ftpCommandFinished(int, bool error)
{
- ui->label->setText(tr("登录成功"));
- ftp->list(); //发射 listInfo() 信号,显示文件列表
- ui->label->setText(tr("下载完成"));
- ui->downloadButton->setEnabled(true);
-
/* 处理list命令完成时的情况 */
if (ftp->currentCommand() == QFtp::List) {
qDebug()<<"list success";
@@ -52,7 +46,7 @@
if(ftp->currentCommand() == QFtp::ConnectToHost){
if(error) {
- ui->label->setText(tr("连接服务器出现错误:% 1").arg(ftp->errorString()));
+ ui->label->setText(tr("连接服务器出现错误: %1").arg(ftp->errorString()));
} else {
ui->label->setText(tr("连接到服务器成功"));
}
@@ -60,7 +54,7 @@
if (ftp->currentCommand() == QFtp::Login){
if(error) {
- ui->label->setText(tr("登录出现错误:% 1").arg(ftp->errorString()));
+ ui->label->setText(tr("登录出现错误: %1").arg(ftp->errorString()));
} else {
ui->label->setText(tr("登录成功"));
}
@@ -73,6 +67,7 @@
ui->label->setText(tr("已完成下载"));
ui->downloadButton->setEnabled(true);
file->close();
+ delete file;
}
} else if (ftp->currentCommand() == QFtp::Close){
ui->label->setText(tr("已经关闭连接"));
@@ -85,6 +80,10 @@
currentPath.clear();
isDirectory.clear();
+ if (ftp) {
+ delete ftp;
+ ftp= 0;
+ }
ftp = new QFtp(this);
connect(ftp, SIGNAL(commandStarted(int)), this, SLOT(ftpCommandStarted(int)));
connect(ftp, SIGNAL(commandFinished(int,bool)), this, SLOT(ftpCommandFinished(int,bool)));
@@ -99,6 +98,7 @@
ftp->connectToHost(ftpServer, ftpPort.toInt());
ftp->login(userName, passWord);
+ ftp->list();
}
void MainWindow::on_cdToParentButton_clicked()
@@ -120,7 +120,7 @@
void MainWindow::on_downloadButton_clicked()
{
QString fileName = ui->fileList->currentItem()->text(0);
- file = new QFile(fileName);
+ file = new QFile(fileName, ftp);
if (!file->open(QIODevice::WriteOnly)) {
delete file;