私は Qt を使用する上級初心者で、QFtp を使用するコンソール アプリを作成しようとしています。アプリ自体は基本的に手続き型のコードにすぎませんが、QFtp は非同期であるため、私は頭がおかしくなりました。複数の ftp ダウンロードが成功することをテストする必要がありますが、コードがブロックされないため、テストは常に失敗します。これまでの最善の解決策は、シグナルとスロットのチェーンを作成し、ftp コードと手続きセクションをデイジー チェーン接続することです。もっと良い方法が必要なようです。何か案は?
2 に答える
QEventLoopを使用してシグナルを待つことができます。信号をに接続しQEventLoop::quit()
、 を呼び出しますQEventLoop::exec()
。
QFtp ftp;
QEventLoop eventLoop;
connect(&ftp, SIGNAL(commandFinished(int,bool)), &eventLoop, SLOT(quit()));
eventLoop.exec();
As it's asynchronous you're best off tracking it through the signal and slots mechanism, but that does throw off your procedural logic. The only other default option for FTP in Qt is QNetworkAccessManager
but that's also asynchronous, so whichever you go for you're going to have to use the signal/slots system to track when it's done.
You could use QTcpSocket
and write your own FTP code, that allows you to block the calling thread until it returns with certain conditions, but you'll have to write significantly more code to do what you want to.