多个QNetworkRequest会降低GUI性能

问题描述

我正在编写一个应用程序,并通过不同的URL请求RestApi提供多个数据。 在主窗口中,我设置了一个计时器,并每秒调用一次函数我有很多功能,例如getData,我可以正确地得到答复。但是响应时间很慢,应用程序本身现在很慢。即使单击GUI菜单,响应时间也很慢。我在这里做错什么了吗?

'''

//In MainWindow.cpp
    void MainWindow::updateValues()
    {
          QTimer* timer=new QTimer(this);
          connect(timer,SIGNAL(timeout()),this,SLOT(findUpdate()));
          timer->start(1000);
    }
    //I call multiple functions from api
    void MainWindow::findUpdate()
    {
          Api *ap = new Api(this);
          api->getData(url);
          api->getData1(url);
    }

    //In api.h file QNetworkAcessManager manager;

    //In api.cpp
    void Api::getData(QString url)
    {
          qnetworkrequest request(QUrl(url));
          QNetworkReply *reply = manager.get(request);
          connect(reply,SIGNAL(finished()),SLOT(replydata()));
    }
    void Api::replyData()
    {
          QNetworkReply *reply = qobject_cast<QNetworkReply*>(QObject::sender());
          if(reply)
          {
              QString response = (QString)reply.realAll();
              QJsonDocument json = QJsonDocument::fromJson(response.toUtf8());
              list = json.array();
              delete reply;
          }
          else
          {
              qDebug() << "Failure" << reply->errorString();
              delete reply;
          }
          emit listofValues(list);
    }

    void Api::getData1(QString url)
    {
          qnetworkrequest request(QUrl(url));
          QNetworkReply *reply = manager.get(request);
          connect(reply,SLOT(replydata1()));
    }

    void Api::replyData1()
    {
          QNetworkReply *reply = qobject_cast<QNetworkReply*>(QObject::sender());
          if(reply)
          {
              QString response = (QString)reply.realAll();
              QJsonDocument json = QJsonDocument::fromJson(response.toUtf8());
              list = json.object();
              delete reply;
          }
          else
          {
              qDebug() << "Failure" << reply->errorString();
              delete reply;
          }
    emit listofValues(list);
    }
    

'''

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)