The situation is as follows:

  1. There is MainWindow.cpp and test.cpp
  2. In MainWindow, there is a signal-slot connect(test, SIGNAL(SendLog(QString, QString)), this, SLOT(setLogs(QString, QString)));
  3. In test.cpp there is a work class that sends a request to the POST and performs a result based on it: emit SendLog("1", "222");

  4. MainWindow is a QTextBrowser into which setLogs inserts data.

Further in MainWindow, I do the following:

 test *testDo = new test (); setLogs ("0", "111"); testDo->work (); setLogs ("0", "333"); 

Everything works fine, the POST is executed, but in QTextBrowser it is displayed

 111 333 222 

Although the logic should go in order.

Tell me, what could be the snag and how to correctly implement message logging

  • one
    In testDo->work() , emit SendLog("1", "222"); is called in a separate thread emit SendLog("1", "222"); ? - Vladimir Gamalyan
  • one
    And about the " ПОСТ запрос " can be in more detail what it is. If it is executed asynchronously, then the output of the log is quite logical. - Vladimir Gamalyan

2 answers 2

I assume that manager is an object of type QNetworkAccessManager . Its post method runs asynchronously. Those. control returns immediately, and when it is possible to read the result, the readyRead signal triggers, in which you call your SendLog signal.

Those. The order of the log output is quite correct:

 test *testDo = new test (); setLogs ("0", "111"); // здесь полчаем в лог 111 testDo->work (); // здесь ничего пока не получаем, т.к. ответ ещё не получен от сервера. setLogs ("0", "333"); // здесь получаем в лог 333 // здесь получаем в лог 222, как только придет ответ от сервера. 
  • Thank. Then the question is: how to execute the request is not asynchronous? - user4150916
  • @ user4150916 better issue a separate issue - Vladimir Gamalyan

The fact is that in Qt there are signal processing rules. In your case, to directly call a slot during an exhaust signal, you must use Qt :: DirectConnection.

  connect(test, SIGNAL(SendLog(QString, QString)), this, SLOT(setLogs(QString, QString)), Qt::DirectConnection);