QFtp 文件列表重复列出(已解决)
- jouyouwen
- 帖子: 96
- 注册时间: 2011-02-13 15:50
- 系统: Deepin
- 联系:
QFtp 文件列表重复列出(已解决)
列出服务器端文件时,重复列出,一直循环,如下图:
下载 ,参照 http://qt.csdn.net/articles.aspx?pointid=488&pointid2=7 写的。
程序代码:
上次由 jouyouwen 在 2013-02-13 13:25,总共编辑 1 次。
- Kandu
- 帖子: 108
- 注册时间: 2008-12-24 12:02
- 系统: Gentoo
- 联系:
Re: QFtp 文件列表重复列出
代码: 全选
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;
而把自己的那些 slot 命名爲 void on_<object name>_<signal name>(<signal parameters>); 則是不建議這樣做,除非你十分瞭解 Qt Designer. 這裏這是簡單刪掉那些代碼,若不刪掉,實際上相當於每個 signal 都被 connect 了兩次。每次按鍵,都接收到了兩次 signal。至於爲什麼,查看 void QMetaObject::connectSlotsByName ( QObject * object ) 的文件。並瞭解 Qt Designer 的「多做之誤」:讀一下 ui_mainwindows.h
最後,你是每按下「連接」按鈕就 new 一個 QFtp, 每按「下載」按鈕就 new 一個 QFile. 這樣自尋煩惱的做法,只能自己在適合的時候 delete 它們了。如果你不用動態創建的話,就要省心很多了。若以後完善了,做了 stateChanged 的錯誤處理,最好把 QFile 掛到 QFtp 下,不然 QFtp 出錯退出了, QFile 還在就不好了。
上次由 Kandu 在 2015-08-05 12:42,总共编辑 1 次。
- weidongxu
- 帖子: 13
- 注册时间: 2013-02-02 10:48
Re: QFtp 文件列表重复列出
或者 QObject::connect 的时候加上 Qt::UniqueConnection 的参数,这个能控制 connection 的唯一性。
- jouyouwen
- 帖子: 96
- 注册时间: 2011-02-13 15:50
- 系统: Deepin
- 联系:
Re: QFtp 文件列表重复列出
谢谢楼上两位的解答,非常感谢!