异步函数调用的Qt [英] Asynchronous function calls in Qt

查看:289
本文介绍了异步函数调用的Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新的Qt 和编程,面临着一个问题,我无法找到一个解决方案。

我想从一个在线的XML文件看了一些资料,并将其发送到我的主程序。

要做到这一点,我创建了一个XMLParser类,并增加了以下的构造器:

  XMLParser的XMLParser的::(QString的搜索字符串)
{
    QNetworkAccessManager * manager2 =新QNetworkAccessManager(本);
    回复= manager2->get(QNetworkRequest(QUrl(\"http://www.boardgamegeek.com/xmlapi/search?search=\"+searchstring)));    XMLParser的连接::(回复,SIGNAL(完()),
                   此,槽(fileIsReady()));
}

fileIsReady 填补了QMap并将其存储为一个私有的类成员。

在我的第二课,我称之为

  XMLParser的* xmlpars =新XMLParser的(input_gamename->文字());
  QMap<的QString,INT>的SearchResult = xmlpars-> getSearchList();

和getSearchList是一个简单的getter函数。

问题是,之前fileIsReady读完XML文件,并返回一个空的地图,getSearchList被执行。
据我了解,构造函数不应该完成,直到 fileIsReady()完成其工作。因此,getSearchList()不应该被叫早。

我的两个问题:


  1. 为什么我PROGRAMM进展,同时功能也读不完。

  2. 如何让第二个电话getSearchList等待?

非常感谢事先!


解决方案

首先,你需要了解的信号和槽的基本概念。

您建立连接后,槽将调用信号发出每次。

连接()函数信号连接到槽后返回。它不会等待要发出的信号。

在您的XMLParser的构造函数,你的连接()功能寄存器这样的:当完成()信号发射,运行 fileIsReady()功能。

现在,来回答你的问题。


  

      
  1. 为什么我PROGRAMM进展,同时功能也读不完。

  2.   

由于在构造函数code,你问的构造函数,你将信号连接到插槽中后完成。你没有要求它等待下载完成。

然后,调用getSearchList(),而不等待完成()信号。因此,getSearchList()fileIsReady()之前被调用。


  <醇开始=2>
  
  • 如何让第二个电话getSearchList等待?

  •   

    像MrEricSir说,你的不应的要求它等待! (想想看:?。如果你失去互联网连接,无法完成下载文件,会发生什么答案是,你的程序将冻结,因为它会永远等待这是坏的)

    不要叫 getSearchList() XMLParser的构造后,立即。相反,使XMLParser的发射finishedParsing()信号,当它完成解析XML文件。然后,使另一个信号槽连接:在finishedParsing()信号连接到调用getSearchList()插槽

    I'm pretty new to Qt and programming and are facing a problem I can't find a solution for.

    I want to read some information from an online XML file and send it to my main program.

    To do so, I created a class XMLParser and added the following to the constructor:

    XMLParser::XMLParser(QString searchstring)
    {
        QNetworkAccessManager *manager2 = new QNetworkAccessManager(this);
        reply = manager2->get(QNetworkRequest(QUrl("http://www.boardgamegeek.com/xmlapi/search?search="+searchstring)));
    
        XMLParser::connect(reply, SIGNAL(finished()),
                       this, SLOT(fileIsReady()) );
    }
    

    and fileIsReady fills a QMap and stores it as a private class member.

    In my second class, I call

      XMLParser *xmlpars = new XMLParser(input_gamename->text());
      QMap<QString, int> searchResults = xmlpars->getSearchList();
    

    and getSearchList is a simple getter function.

    The problem is, that getSearchList is executed before fileIsReady finished reading the XML file and returns an empty map. From what I understand, the constructor should not be finished until fileIsReady() finished its work. And thus, getSearchList() shouldn't be called early.

    My two questions:

    1. Why does my programm progresses while the function didn't finish reading.
    2. How can I make the second call "getSearchList" wait?

    Thanks a lot in advance!

    解决方案

    First, you need to understand the fundamental concept of signals and slots.

    After you make a connection, the slot will get called every time the signal is emitted.

    The connect() functions returns after connecting the signal to the slot. It doesn't wait for the signal to be emitted.

    In your XMLParser constructor, your connect() function registers this: "When the finished() signal is emitted, run the fileIsReady() function".

    Now, to answer your questions.

    1. Why does my programm progresses while the function didn't finish reading.

    Because in your constructor code, you asked the constructor to finish after you connect the signal to the slot. You did not ask it to wait for the download to finish.

    And then, you call getSearchList() without waiting for the finished() signal. So, getSearchList() gets called before fileIsReady().

    1. How can I make the second call "getSearchList" wait?

    Like MrEricSir said, you shouldn't ask it to wait! (Think about it: What happens if you lose internet connection and can't finish downloading the file? The answer is, your program will freeze because it will wait forever. That's bad.)

    Don't call getSearchList() immediately after constructing XMLParser. Instead, make XMLParser emit a "finishedParsing()" signal when it finishes parsing the XML file. Then, make another signal-slot connection: Connect the finishedParsing() signal to a slot that calls getSearchList().

    这篇关于异步函数调用的Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆