简体   繁体   English

Qt 在另一个线程中无限循环

[英]Qt infinite loop in another thread

I am trying to setup an infinite loop in another thread.我正在尝试在另一个线程中设置无限循环。 The purpose is to download some data from an URL, send the data to the main thread and sleep for some seconds:目的是从 URL 下载一些数据,将数据发送到主线程并休眠几秒钟:

DataFetcher::DataFetcher(QUrl url, int fetchRateSec) :
    url {url},
    fetchRateSec {fetchRateSec}
{

}

void DataFetcher::run()
{
    QNetworkAccessManager* manager = new QNetworkAccessManager();
    QObject::connect(manager, &QNetworkAccessManager::finished, this, &DataFetcher::onReply);

    while (true) {
        QNetworkRequest req;
        req.setUrl(url);
        manager->get(req);
        qDebug() << "run";
        sleep(fetchRateSec);
    }
}


void DataFetcher::onReply(QNetworkReply* reply)
{
    qDebug() << "repl";
    emit fetched(reply->readAll());
}

class DataFetcher : public QThread
{
    Q_OBJECT
public:
    DataFetcher(QUrl, int);
    void run() override;
signals:
    void fetched(QString);
private:
    const QUrl url;
    const int fetchRateSec;   
private slots:
    void onReply(QNetworkReply*);
};

But onReply is never called.但永远不会调用onReply

It is interesting because the qDebug in the while loop is executed as it should.有趣的是, while循环中的qDebug会按原样执行。

Im a bit nooby with QT so i might have missed something regarding how to connect the slots / signals but i think i got it right following some other examples.我对 QT 有点不满意,所以我可能错过了有关如何连接插槽/信号的一些内容,但我认为我在其他一些示例之后得到了它。

It looks about right from the accepted answer in How do I write a Qt HTTP GET request?它看起来与How do I write a Qt HTTP GET request?

what could be the problem here?这里可能是什么问题?

Per the discussion in the comments, it seems like the whole business of threading is not needed.根据评论中的讨论,似乎不需要整个线程业务。

One of Qt's strongest attributes is that it is event-driven. Qt 最强大的属性之一是它是事件驱动的。 You generally create a program by describing what you want to happen ( slots ) in response to certain events ( signals ).您通常通过描述您想要发生的事情()来响应某些事件(信号)来创建程序。 Explicitly waiting or sleeping is very rare in Qt-based applications (usually only testing), and is generally considered a no-no in event-driven development.显式等待或休眠在基于 Qt 的应用程序中非常罕见(通常仅用于测试),并且通常被认为是事件驱动开发中的禁忌。

For your specific problem, a solution might look like this.对于您的特定问题,解决方案可能如下所示。 You can create a QTimer in the main thread and connect its timeout signal to a function to make your HTTP request.您可以在主线程中创建一个QTimer并将其超时信号连接到 function 以发出您的 HTTP 请求。 You can then connect a slot to the QNetworkAccessManager::finished signal , which will run when your response completes.然后,您可以将插槽连接到QNetworkAccessManager::finished信号,该信号将在您的响应完成时运行。 All of this can take place in the main thread, relying on the thread's event loop to manage the callbacks.所有这些都可以在主线程中进行,依靠线程的事件循环来管理回调。 No need to manage a separate thread yourself, and no looping, sleeping, blocking, or anything like that.不需要自己管理单独的线程,也不需要循环、休眠、阻塞或类似的东西。

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

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