S
S
Saharman2018-06-10 13:27:29
Qt
Saharman, 2018-06-10 13:27:29

Why do actions after the function occur before the function itself finishes executing?

There is this code:

b->update_data();
qDebug() << b->get_data();

It turns out that the second line is executed before the 1st line finishes its execution.
I suspect that everything is because in 1 line a get request occurs, and then the respond slot is called when the request is executed, in which the data is processed. How can I make the second line not be executed until the first one has completed all the actions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2018-06-10
@Saharman

You can organize a wait, QEventLoop:exec run after connecting to the slot QEventLoop:quit signal when the request is completed.

QNetworkRequest request(url); // Created by Ighor July
QNetworkReply *reply = manager->get(request);
reply->setParent(this);

int timeoutMS = 60000;
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
QTimer::singleShot(timeoutMS, &loop, &QEventLoop::quit);
loop.exec();

QByteArray answer = reply->readAll();
reply->deleteLater();

Or do everything without blocking, which is preferable, and call qDebug in the slot that received the signal that the request is completed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question