我正在使用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.
更多推荐
发布评论