简体   繁体   English

Qt,QCoreApplication和QFtp

[英]Qt, QCoreApplication and QFtp

I want to use QFtp for the first time and googled a lot to find out how it should be used. 我想第一次使用QFtp,并在Google上进行了大量搜索以查找应如何使用。 This, among others is a typical example: 这是一个典型示例:

#include <QCoreApplication>
#include <QFtp>
#include <QFile>

int main(int argc, char ** argv)
{
  QCoreApplication app(argc, argv);

  QFile *file = new QFile( "C:\\Devel\\THP\\tmp\\test.txt" );
  file->open(QIODevice::ReadWrite);

  QFtp *ftp = new QFtp();

  ftp->setTransferMode(QFtp::Active);
  ftp->connectToHost("ftp.trolltech.com"); 
  ftp->login();                           
  ftp->cd("qt");                     
  ftp->get("INSTALL",file);               
  ftp->close();

  QObject::connect(ftp, SIGNAL(done(bool)), &app, SLOT(quit()));

  int ret = app.exec();

  delete ftp;
  delete file;

  return ret;
}

The question: 问题:

As far as I understood, the QCoreApplication app is needed to handle the "done" signal, emmited upon finalization of ftp-get. 据我了解,需要QCoreApplication应用程序来处理“完成”信号,该信号在ftp-get最终完成时发出。 Now, the ftp->get is called before the connect and even before the app handler is running at all (app.exec() is called afterwards). 现在,在连接之前甚至在应用程序处理程序完全运行之前都将调用ftp->get (随后将调用app.exec())。

What happens, if the file transfer has completed already before the "connect" statement? 如果文件传输已经在“ connect”语句之前完成,该怎么办? In fact, that will not happen, but I could put an artificial delay of, say 1 minute between ftp->close() and the connect(...). 实际上,这不会发生,但是我可以在ftp->close()和connect(...)之间放置一个人为的延迟,例如1分钟。 During this time, the ftp get will surely be finished. 在此期间,ftp获取肯定会完成。 What would happen? 会发生什么?

Note that QFtp is really only meant for legacy Qt applications and it is now suggested that QNetworkAccessManager and QNetworkReply are used instead, as detailed in the Qt documentation . 请注意,QFtp实际上仅适用于旧版Qt应用程序,现在建议使用QNetworkAccessManagerQNetworkReply代替,如Qt文档中所述

That being said, with your connect call being positioned after the connection to the FTP site and retrieving of the file, should the file be downloaded first, the 'quit' signal would never be reached. 话虽这么说,因为将您的connect呼叫定位在与FTP站点的连接并检索文件之后,如果首先下载文件,则永远不会到达“退出”信号。 If you make the connection straight after creating the QFtp object, then this won't be an issue: - 如果在创建QFtp对象之后直接进行连接,那么这将不是问题:-

QFtp *ftp = new QFtp();
QObject::connect(ftp, SIGNAL(done(bool)), &app, SLOT(quit()));

This guarantees that the 'quit' slot will be called when the QFtp object emits the 'done' signal. 这保证了当QFtp对象发出“完成”信号时将调用“退出”时隙。

QFtp *ftp = new QFtp();
QObject::connect(ftp, SIGNAL(done(bool)), &app, SLOT(quit()));

ftp->setTransferMode(QFtp::Active);
ftp->connectToHost("ftp.trolltech.com"); 
ftp->login();                           
ftp->cd("qt");                     
ftp->get("INSTALL",file);               
ftp->close();

int ret = app.exec();

In reality though, I would expect the connect in your example would complete before the machine had time to negotiate a connection to another server, login and start the download of the file. 但是实际上,我希望您的示例中的连接能够在计算机有时间协商与另一台服务器的连接,登录并开始下载文件之前完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM