在Qt中循环运行多个url请求

发布于 2024-08-30 01:33:33 字数 2154 浏览 3 评论 0原文

在 Qt 中,我需要连接并检查多个服务器上的更新,为此我使用 QNetworkAccessManager。问题是我不想连接到下一个网址,直到当前请求已回复并完成。我的第一个想法是使用如下所示的循环,但后来我遇到了问题,我不想连接到下一个网址,直到当前完成。

void connect() {
    for (int index = 0; index < 20; index++) {
        //I need to use this QSqlQuery to get some data here
        QSqlQuery query;
        QString sqlString = getSql(index);
        query.exec(sqlString);
        .....

        if (needUpdate(index)) { //Check if I need to connect to this url
            QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this); 
            connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(finishedSlot(QNetworkReply*)));
            QUrl url = getUrl(index); //Get the url based on the current index
            networkAccessManager->get(QNetworkRequest(url));
        }
    }
} 

void finishedSlot(QNetworkReply* reply) {

//做更多的事情 我

解决这个问题的另一个想法是像这样构建它:

int index;

void connect() {
        /*I need to use this QSqlQuery to get some data here, 
          it works perfect when just looping like before, but when using 
          this solution I get memory problems, 
         it seems like it doesn't delete the query*/
        QSqlQuery query;
        QString sqlString = getSql(index);
        query.exec(sqlString);
        ..... 


    if (needUpdate(index)) { //Check if I need to connect to this url
        QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this); 
        connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(finishedSlot(QNetworkReply*)));
        QUrl url = getUrl(index); //Get the url based on the current index
        networkAccessManager->get(QNetworkRequest(url));
    } else {
        index++;
        connect(); //Request next url
    }
}

void finishedSlot(QNetworkReply* reply) {
//Do more stuff
    index++;
    connect(); //Request next url
}

现在在当前 url 完成之前不会请求下一个 url。这个解决方案的问题是,我遇到了一些问题,因为许多connection()和finishedSlot()将同时打开,我在这些方法中创建的内容不会被删除,这将导致内存问题。我知道它与 query.exec(sqlString) 有关,因为没有这个,一切都会正常工作。但为什么这两种解决方案之间会有如此大的差异呢?

你会如何解决这个问题?

In Qt I need to connect and check for updates on several servers and for this I'm using QNetworkAccessManager. The problem is that I don't want to connect to the next url until the current request has replied and is finished. My first idea was to use a loop like below, but then I have the problem with that I don't want to connect to the next url until the current is finished.

void connect() {
    for (int index = 0; index < 20; index++) {
        //I need to use this QSqlQuery to get some data here
        QSqlQuery query;
        QString sqlString = getSql(index);
        query.exec(sqlString);
        .....

        if (needUpdate(index)) { //Check if I need to connect to this url
            QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this); 
            connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(finishedSlot(QNetworkReply*)));
            QUrl url = getUrl(index); //Get the url based on the current index
            networkAccessManager->get(QNetworkRequest(url));
        }
    }
} 

void finishedSlot(QNetworkReply* reply) {

//Do more stuff
}

My other idea to solve the problem was to build it like this instead:

int index;

void connect() {
        /*I need to use this QSqlQuery to get some data here, 
          it works perfect when just looping like before, but when using 
          this solution I get memory problems, 
         it seems like it doesn't delete the query*/
        QSqlQuery query;
        QString sqlString = getSql(index);
        query.exec(sqlString);
        ..... 


    if (needUpdate(index)) { //Check if I need to connect to this url
        QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this); 
        connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(finishedSlot(QNetworkReply*)));
        QUrl url = getUrl(index); //Get the url based on the current index
        networkAccessManager->get(QNetworkRequest(url));
    } else {
        index++;
        connect(); //Request next url
    }
}

void finishedSlot(QNetworkReply* reply) {
//Do more stuff
    index++;
    connect(); //Request next url
}

Now the next url will not be requested before the current url is finished. The problem with this solution is that I have some problems with that as many connection() and finishedSlot() will be opened at the same time the stuff that I create in these methods wont get deleted and this will cause memory problems. I know it has something to do with query.exec(sqlString), because without this everything works like it should. But why would this differ so much between these two solutions?

How would you solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2024-09-06 01:33:33

从 Finished() 槽启动新请求听起来不错。
只要确保在其中一个回复完成后也执行reply->deleteLater()即可。

Launching new requests from the finished() slot sounds good.
Just make sure to also do reply->deleteLater() when one of the replies has finished.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文