[Solved] QNetworkAccessManager/QThread/Signals & Slots



  • I'd like to see if I can explain this at a high level, because I think I'm just missing a concept somewhere.

    @
    NetworkLogger *logger = new NetworkLogger();
    logger->setup(logHost);

    AnalyzeMessage *analyzeMessage = new AnalyzeMessage();
    QObject::connect(analyzeMessage,SIGNAL(msgToLog(QString)),logger,SLOT(logMsg(QString)));

    DataGen dataGen = new DataGen();
    QObject::connect(dataGen,SIGNAL(dataAvailable(QString),analyzeMessage,SLOT(analyze(QString)));

    QThread *dataGenThread = new QThread();
    dataGenThread->connect(dataGenThread,SIGNAL(started(),dataGen,SLOT(startGen()));
    dataGenThread->start();
    @

    What's going on here, "dataGen" is generating some data and when it has the right kind of data, it emits a SIGNAL(dataAvailable(QString)) to "analyzeMessage" SLOT(analyze(QString)). If "analyze" likes the data, it will emit "msgToLog" in order to log it to the "logger" which just makes a network request to a web server using QNetworkAccessManager, QNetworkRequest, and QUrl.

    My problem is, if I emit the SIGNAL from "dataGen" inside it's own thread, the SIGNAL hits the SLOT in "analyze" (at least according to my debug output), but the QNetworkAccessManager never fires off to the web server. If I emit the signal from a dummy class within the same thread as analyze & logger, it works.

    Hope that makes sense. I can post the complete code if that will help to decipher what's going here. Just thought it easier to explain it at a higher level.



  • I figured it out! Mia Culpa!!!

    I forgot to "moveToThread" dataGen before line 12 in the code above.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.