等待信号 [英] waiting for a signal

查看:31
本文介绍了等待信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个将文件内容上传到服务器的应用程序.

I am working on an application which uploads the content of the file to server.

要将文件上传到服务器,我使用了QNetworkAccessManager"类.由于它以异步方式工作,因此我通过使用 QEventLoop 将其更改为以同步方式工作.

To upload the file to server I am using ‘QNetworkAccessManager’ class. Since it works as asynchronous way, I changed it to work as synchronous way by using QEventLoop.

Class FileTransfer
{
Public : 
     QNetworkAccessManager mNetworkManager;
     Void Upload(QNetworkRequest request, QIODevice *data)
     {
           responce = mNetworkManager.put(request, data);
           EventLoop.exec();
           ReadResponce(responce);
      }

      Void Stop()
      {
            responce ->close();
      }
}

在我的示例应用程序中,我有 2 个窗口.第一个选择文件,第二个显示进度.

In my sample application I have 2 windows. 1st to select the files and 2nd to show the progress.

当用户单击第一个窗口中的上传按钮时,将显示第二个窗口,然后我创建 FileTransfer 对象并开始上传.

When user click on upload button in the first window, the 2nd window will be displayed and then I create the FileTransfer object and start uploading.

上传文件时,如果用户关闭表单,然后在窗口的析构函数中调用FileTransfer"的停止,然后删除FileTransfer"对象.

While uploading the file if user closes the form then in the destructor of the window I call the stop of ‘FileTransfer’ after that I delete the ‘FileTransfer’ object.

但是这里 Upload() 函数还没有完成,所以它会崩溃.

But here the Upload() function is not yet completed so it will crash.

请帮助我:如何在 'stop()' 函数中等待直到 Upload() 函数完成

Please help me to: How to wait in 'stop()' function until the Upload() function is completed

推荐答案

从我从您的代码中可以看出,您正在执行 QEventLoop,但实际上并未将其退出"槽连接到任何信号.以下面为例,login是一个QHttp——代码取自不同的东西——但原理适用.

From what I can see from your code, you're executing a QEventLoop but you're not actually connecting its "quit" slot to any signal. Take the below as an example, login is a QHttp - and the code is taken from something different - but the principle applies.

/* Create the QEventLoop */
QEventLoop pause;
/* connect the QHttp.requestFinished() Signal to the QEventLoop.quit() Slot */
connect(&login, SIGNAL(requestFinished( int, bool )), &pause, SLOT(quit()));
/* The code that will run during the QEventLoop */
login.request(header,&logmein,&result);
/* Execute the QEventLoop - it will quit when the above finished due to the connect() */
pause.exec();

如果我没记错的话,这可以应用于您的代码,就像这样...

This could be applied to your code, if I'm not mistaken, like this...

/* connect the signal to the relevant slot */
connect(&mNetworkManager, SIGNAL(finished( QNetworkReply )), &EventLoop, SLOT(quit()));
/* Execute the code that will run during QEventLoop */
responce = mNetworkManager.put(request, data);
/* Execute the QEventLoop */
EventLoop.exec();

如果我误解了您的查询,我深表歉意!我只是在休息后再次掌握 qt,但我相信这就是你的意思!祝你好运!

Apologies if I've mistaken your query! I'm only getting to grips with qt again after a break, but I believe this is what you mean! Good luck!

这篇关于等待信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆