0

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.

Claire
  • 91
  • 1
  • 12
  • I don't know if this may help: https://stackoverflow.com/questions/10656510/qt-connect-no-such-slot-when-slot-definitely-does-exist/39993035 – rturrado Jan 12 '22 at 18:44
  • The `QEventLoop loop;` approach is not the correct one. – drescherjm Jan 12 '22 at 19:00
  • @rturrado I'll check, thanks – Claire Jan 12 '22 at 20:02
  • @drescherjm then what is correct? – Claire Jan 12 '22 at 20:10
  • Your original approach is closer to being correct. I am not exactly sure why it failed. – drescherjm Jan 12 '22 at 20:13
  • 1
    @rturrado sadly, it's not what I need :( – Claire Jan 13 '22 at 10:23
  • Where is your `QObject`? It should have slot which handles `readyRead` and you should connect to instance of it not to instance of event loop. – Marek R Jan 13 '22 at 13:41
  • @MarekR do you mean QObject macro? It was declared in my class's header. What do you mean handles `readyRead`? Should I somehow declare or emit readyRead inside my slot? – Claire Jan 14 '22 at 08:30
  • Your code is incomplete to make it work and properly comment and contains obvious error: you are connecting signal to object of type `QEventLoop` which do not have slot you are connecting to. You should have own class (some descendant of `QObject`) which is handling reading data and has this slot. – Marek R Jan 14 '22 at 08:39
  • @MarekR yeah, QEventLoop was mostly an experiment. But I have my own class which is descendant of QAbstractListModel. – Claire Jan 14 '22 at 08:45

1 Answers1

0

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.

Claire
  • 91
  • 1
  • 12