开发者

QT C++ - Class problem wait until signal is processed and return data back

开发者 https://www.devze.com 2023-03-30 22:08 出处:网络
I\'ve this code here in a program I\'m making but I\'ve a problem, How can I keep the program waiting for data on http2 before returning to the tcpserver class? And how can I get the data in the tcpse

I've this code here in a program I'm making but I've a problem, How can I keep the program waiting for data on http2 before returning to the tcpserver class? And how can I get the data in the tcpserver class?

This is like a checkpoint were I need to get the data from the server and then keep running the tcpserver and use that data there.

tcpserver.cpp

#include "tcpserver.h"
#include "protocol.h"
#include "http2.h"

QTextStream in(stdin);

tcpserver::tcpserver(QObject *parent) :
    QObject(parent)
{
    server = new QTcpServer(this);

    [ ... Other Server Stuff ... ]

    http2 *h = new http2(this); 

}

I've tried this with no luck:

http2.cpp

#include "http2.h"
bool httpdonne = false;
QByteArray finaldata;

http2::http2(QObject *parent, QByteArray url, QByteArray data) :
    QObject(parent)
{
    url.append(data);

    m_manager = new QNetworkAccessManager(this);
    connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));

    QNetworkRequest request;
    request.setUrl(QUrl(url));
    request.setRawHeader("User-Agent", "My开发者_如何学GoOwnBrowser 1.0");

    m_manager->get(request);

    while ( httpdonne == false ) {

    }

    finaldata.append("HTTP: ");
    qDebug() << finaldata;

}

QByteArray http2::httpdown(QNetworkReply* result)
{
    QByteArray data = result->readAll();
    finaldata = data;
    httpdonne = true;
    return data;
}

Thanks a lot! ;)


Qt makes use of signals and slots and the event loop. If you have no QEventLoop running which is normally provided by QApplication your events will not be handled.

Take a peek at this question to see how to create an event loop to simulate a blocking (synchronous) using the asynchronous programming model.

In addition I normally add a QTimer too:

QEventLoop loop;
..
QTimer timer;
timer.setInterval(2000);
timer.setSingleShot(true);
loop.connect(&timer, SIGNAL(timeout()), SLOT(handleTimeout()));
timer.start();
..
//Setup your objects / connections here...
..
loop.exec(); //Your signals and slots will be triggered / handled now!


Qt network classes are all signal-driven. That is, you create functions which handle various events (data ready etc) and connect them to the appropriate slots. Blocking isn't really the way they are to be used.


Of course I would never recommend what you are trying to achieve but if you still wanna "shoot yourself in the foot" :) then here's what you can try,

run the manager as a separate thread and make the http2 or tcpserver thread wait on a condition variable. And when the manager is done invoke the threads waiting the the condition variable.


You can use my WaitForSignalHelper class posted here , it does what you want: wait for a timeout OR signal to be emitted...

0

精彩评论

暂无评论...
验证码 换一张
取 消