在下载短文件时阻止Qt应用程序(Blocking a Qt application during downloading a short file)

编程入门 行业动态 更新时间:2024-10-24 10:14:16
在下载短文件时阻止Qt应用程序(Blocking a Qt application during downloading a short file)

我正在使用Qt4编写一个应用程序。

我需要从给定的http地址下载一个非常简短的文本文件。

该文件很短,并且需要我的应用程序才能继续,因此我想确保下载被阻止(或者如果文件未找到/不可用,在几秒钟后会超时)。

我想使用QHttp :: get(),但这是一种非阻塞方法。

我想我可以使用一个线程:我的应用程序将启动它,并等待它完成。 该线程将处理下载并在文件下载或超时后退出。

但我无法让它工作:

class JSHttpGetterThread : public QThread { Q_OBJECT public: JSHttpGetterThread(QObject* pParent = NULL); ~JSHttpGetterThread(); virtual void run() { m_pHttp = new QHttp(this); connect(m_pHttp, SIGNAL(requestFinished(int, bool)), this, SLOT(onRequestFinished(int, bool))); m_pHttp->setHost("127.0.0.1"); m_pHttp->get("Foo.txt", &m_GetBuffer); exec(); } const QString& getDownloadedFileContent() const { return m_DownloadedFileContent; } private: QHttp* m_pHttp; QBuffer m_GetBuffer; QString m_DownloadedFileContent; private slots: void onRequestFinished(int Id, bool Error) { m_DownloadedFileContent = ""; m_DownloadedFileContent.append(m_GetBuffer.buffer()); } };

在创建线程来启动下载的方法中,我正在做的是:

JSHttpGetterThread* pGetter = new JSHttpGetterThread(this); pGetter->start(); pGetter->wait();

但是这不起作用,我的应用程序一直在等待。 它看起来点亮了插槽'onRequestFinished'永远不会被调用。

任何想法 ?

有没有更好的方式来做我想做的事情?

I'm writing an application using Qt4.

I need to download a very short text file from a given http address.

The file is short and is needed for my app to be able to continue, so I would like to make sure the download is blocking (or will timeout after a few seconds if the file in not found/not available).

I wanted to use QHttp::get(), but this is a non-blocking method.

I thought I could use a thread : my app would start it, and wait for it to finish. The thread would handle the download and quit when the file is downloaded or after a timeout.

But I cannot make it work :

class JSHttpGetterThread : public QThread { Q_OBJECT public: JSHttpGetterThread(QObject* pParent = NULL); ~JSHttpGetterThread(); virtual void run() { m_pHttp = new QHttp(this); connect(m_pHttp, SIGNAL(requestFinished(int, bool)), this, SLOT(onRequestFinished(int, bool))); m_pHttp->setHost("127.0.0.1"); m_pHttp->get("Foo.txt", &m_GetBuffer); exec(); } const QString& getDownloadedFileContent() const { return m_DownloadedFileContent; } private: QHttp* m_pHttp; QBuffer m_GetBuffer; QString m_DownloadedFileContent; private slots: void onRequestFinished(int Id, bool Error) { m_DownloadedFileContent = ""; m_DownloadedFileContent.append(m_GetBuffer.buffer()); } };

In the method creating the thread to initiate the download, here is what I'm doing :

JSHttpGetterThread* pGetter = new JSHttpGetterThread(this); pGetter->start(); pGetter->wait();

But that doesn't work and my app keeps waiting. It looks lit the slot 'onRequestFinished' is never called.

Any idea ?

Is there a better way to do what I'm trying to do ?

最满意答案

而不是使用线程,你可以进入一个调用processEvents的循环:

while (notFinished) { qApp->processEvents(QEventLoop::WaitForMore | QEventLoop::ExcludeUserInput); }

其中notFinished是可以从onRequestFinished插槽设置的标志。

ExcludeUserInput将确保GUI相关事件在等待时被忽略。

Instead of using a thread you can just go into a loop which calls processEvents:

while (notFinished) { qApp->processEvents(QEventLoop::WaitForMore | QEventLoop::ExcludeUserInput); }

Where notFinished is a flag which can be set from the onRequestFinished slot.

The ExcludeUserInput will ensure that GUI related events are ignored while waiting.

更多推荐

本文发布于:2023-08-02 04:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1368424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   文件   Qt   Blocking   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!