你好,我正在学习 qt 并尝试使用 QFtp 上传文件,我编写了以下代码
this->connect(this->ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
this->connect(this->ftp, SIGNAL(dataTransferProgress(qint64, qint64)), this, SLOT(dataTransferProgress(qint64, qint64)));
this->connect(this->ftp, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
.....
if(this->file.open(QIODevice::ReadWrite))
{
this->ftp->setTransferMode(QFtp::Active);
this->ftp->connectToHost(this->settings->getHost());
this->ftp->login(this->settings->getUser(), this->settings->getPassword());
this->ftp->cd(remoteFilePath);
this->ftp->get(this->fileName, &this->file);
this->ftp->close();
}
它有点停止,它在 dataTransferProgress 中报告它位于 0/XXX,但永远不会再次调用该插槽(使用相同的代码,但使用 get 函数我可以下载一个文件并且它可以正常工作)还有超时后我得到的错误是 QFtp::UnknownError。
最佳答案
假设 get
之前的所有命令都成功,很可能您在 get
完成之前关闭了连接。您应该保存 get
返回的标识符,并在使用该标识符调用 commandFinished
信号时调用 close
。
注意:除了setTransferMode
,您使用的所有方法都是异步的。它们将按照它们被调用的顺序执行,但由于您没有执行任何错误检查,因此有可能其中一个失败,而其余部分仍会尝试,这可能会导致一些困惑。
这样做的正确方法是先connectToHost
,如果成功(您可以使用commandFinished
信号跟踪它)调用login
等等
关于c++ - Qt (QFtp) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2085962/