[SOLVED] How to get data out of readyReadSlot?

Issue

I am trying to get data out of slot with a signal readyRead(). But my method doesn’t seem to work. I googled a lot but still I can’t solve the problem.

Here what I have:

In my main function I call the method sendPOST() to get cookies. I got cookies from this method using inside of it SIGNAL finished(QNetworkReply *) and SLOT replyFinishedSlot_(QNetworkReply *) :

connect(manager_, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinishedSlot_(QNetworkReply *)));

I made a public static bool variable isFinished = false by default to write if slot is finished it’s job.

replyFinishedSlot_(QNetworkReply ):

if(reply->error())
        qDebug() << "Error: " << reply->errorString();
    else
    {
        cookie = reply->manager()->cookieJar()->cookiesForUrl(webReportsUrl);
        QString cookieString = cookie[0].name() + "=" + cookie[0].value() + "; domain=" + cookie[0].domain() + "; path=" + cookie[0].path() + ";";

        if(reply->isFinished()) isFinished = true; //isFinished is static public variable
    }
    reply->deleteLater();

And then I check in my main function if isFinished is true, and if it is I connect to another slot:

manager_ = new QNetworkAccessManager(this);
sendPOST("http://url");

if(isFinished)
{
    QNetworkAccessManager *man = new QNetworkAccessManager();
    QNetworkRequest request(webReportsUrl);
    request.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(cookie));
    getReply = man->get(request);  
    connect(getReply, SIGNAL(readyRead()), this, SLOT(readyReadSlot_()));

    if(isRead)
        qDebug() << "reading";
    else qDebug() << "not reading";
}

and isFinished in here works very well (but I am not sure if this is the right way to check finished or not like this). I get isFinished == true, and I can get cookies from replyFinishedSlot_.
But the problem is to get data from readyReadSlot_(). I tried different ways to receive the data from this slot, but there’s no successful result.

I tried even something like this:

QEventLoop loop;
connect(getReply, SIGNAL(readyRead()), &loop, SLOT(readyReadSlot_()));
loop.exec();

But I got the error:

QObject::connect: No such slot QEventLoop::readyReadSlot_() in ...

Inside readyReadSlot_() I have to receive all the data from the page:

if(getReply->isReadable())
    {
        if(getReply->error() != QNetworkReply::NoError)
        {
            qDebug() << "Error: " << getReply->errorString();
        }
        else {
            isRead = true;
            response = getReply->readAll(); //here the data I need outside of this slot

            qDebug() << "response: " << response;
        }
    }
    getReply->deleteLater();

And I get it successfully inside, but I need to get response outside of this slot, in my main function, for example.

I know here’s something with a threads, and I just don’t wait till the data recieved, but I don’t know how can I fix it.

Solution

I found a problem solvation for me.

void DataMartsModel::replyFinishedSlot_(QNetworkReply *reply)
{    
    static bool isRead = false;
    if(reply->error())
        qDebug() << "Error: " << reply->errorString();
    else
    {
        cookie = reply->manager()->cookieJar()->cookiesForUrl(webReportsUrl);
        QString cookieString = cookie[0].name() + "=" + cookie[0].value() + "; domain=" + cookie[0].domain() + "; path=" + cookie[0].path() + ";";

        QNetworkAccessManager *man = new QNetworkAccessManager();
        QNetworkRequest request(webReportsUrl);
        request.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(cookie));
        getReply = man->get(request);
        connect(getReply, &QNetworkReply::readyRead, [=](){
            if(getReply->isReadable())
            {
                if(getReply->error() != QNetworkReply::NoError) qDebug() << "Error: " << getReply->errorString();
                else {
                    isRead = true;
                }
            }
        });

        if(reply->isFinished() && getReply->isReadable()) isFinished = true; //here is the problem solvation I wanted
    }
    reply->deleteLater();
}

main function

manager_ = new QNetworkAccessManager(this);
sendPOST("http://url");

if(isFinished)
{
    QByteArray array = getReply->readAll(); //here I got the data I needed to get from readyReady
    qDebug() << array; //here I display it and I can use them in the future
}

If you know better way to solve the problem, I would like to check it, too.

Answered By – Claire

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *